小台的IT备忘录  
脑子越来越不好用,只能依靠烂笔头了~
/// <summary>
/// XML序列化为指定对象
/// Author:taiyonghai
/// Time:2016-08-22
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="xmlString">xml字符串</param>
/// <returns></returns>
public static T XmlDeserialize<T>(string xmlString)
{
    using (StringReader reader = new StringReader(xmlString))
    {
        XmlSerializer ser = new XmlSerializer(typeof(T));
        return (T)ser.Deserialize(reader);
    }
}
/// <summary>
/// 指定对象序列化为XML字符串
/// Author:taiyonghai
/// Time:2016-08-22
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="obj">源对象</param>
/// <returns></returns>
public static string XmlSerializer<T>(object obj)
{
    using (MemoryStream stream = new MemoryStream())
    {
        XmlSerializer xml = new XmlSerializer(typeof(T));
        xml.Serialize(stream, obj);
        using (StreamReader reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
        }
    }
}

 附带一个觉得不错的XML文章:http://www.cnblogs.com/a1656344531/archive/2012/11/28/2792863.html

posted on 2016-09-01 09:54  taiyonghai  阅读(175)  评论(0编辑  收藏  举报