开发者

Using generics with XmlSerializer

When using XML serialization in C#, I use code like this:

public MyObject LoadData()
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(MyObject));
    using (TextReader reader = new StreamReader(settingsFileName))
    {
        return (MyObject)xmlSerializer.Deserialize(reader);
    }
}

(and similar code for deserialization).

It requires casting and is not really nice. Is there a way, directly in .NET Framework, to use generics with serialization? That is to say to write something like:

public MyObject LoadData()
{
    // Generics here.
    XmlSerializer<MyObject> xmlSerializer = new XmlSerializer();
    usi开发者_如何学Pythonng (TextReader reader = new StreamReader(settingsFileName))
    {
        // No casts nevermore.
        return xmlSerializer.Deserialize(reader);
    }
}


An addition to @Oded, you can make the method Generic aswell:

public T ConvertXml<T>(string xml)
{
    var serializer = new XmlSerializer(typeof(T));
    return (T)serializer.Deserialize(new StringReader(xml));
}

This way you don't need to make the whole class generic and you can use it like this:

var result = ConvertXml<MyObject>(source);


Make your serialization class/method generic:

public T LoadData<T>()
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
    using (TextReader reader = new StreamReader(settingsFileName))
    {
        return (T)xmlSerializer.Deserialize(reader);
    }
}


A simple generic wrapper:

public class GenericSerializer<T> : XmlSerializer
{
    public GenericSerializer(): base(typeof(T)) { }
}

This will serialize your object to the bin/debug folder:

static void Main(string[] args)
{
   Person p = new Person { Name = "HelloWorld" };
   GenericSerializer<Person> ser = new GenericSerializer<Person>();
   ser.Serialize(new StreamWriter("person.xml"), p);
}


Try this.

public class SerializeConfig<T> where T : class
{
    public static void Serialize(string path, T type)
    {
        var serializer = new XmlSerializer(type.GetType());
        using (var writer = new FileStream(path, FileMode.Create))
        {
            serializer.Serialize(writer, type);
        }
    }

    public static T DeSerialize(string path)
    {
        T type;
        var serializer = new XmlSerializer(typeof(T));
        using (var reader = XmlReader.Create(path))
        {
            type = serializer.Deserialize(reader) as T;
        }
        return type;
    }
}


always work's for me

  public static string ObjectToXmlSerialize<T>(T dataToSerialize)
    {
        try
        {
            var stringwriter = new System.IO.StringWriter();
            var serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(stringwriter, dataToSerialize);
            return stringwriter.ToString();
        }
        catch (Exception ex)
        {
        }
        return null;
    }

and this is for Deserialize:

  public static T XmlDeserializeToObject<T>(string xmlText)
    {
        try
        {
            var stringReader = new System.IO.StringReader(xmlText);
            var serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stringReader);
        }
        catch (Exception ex)
        {
        }
        return default(T);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜