Thursday, January 12, 2012

Сереализация через generic:
 
 
using System.Xml.Serialization;

public class SerializationHelper<T> where T : class
{
    public string SerializeXml(T o)
    {
        var serializer = new XmlSerializer(typeof(T));

        var sb = new StringBuilder();
        using (var tw = new StringWriter(sb))
        {
            serializer.Serialize(tw, o);
        }

        return sb.ToString();
    }

    public T DeserializeXml(string text)
    {
        var serializer = new XmlSerializer(typeof(T));

        using (var tr = new StringReader(text))
        {
            return (T)serializer.Deserialize(tr);
        }
    }
}

No comments:

Post a Comment