码迷,mamicode.com
首页 > Windows程序 > 详细

C# XML序列化

时间:2016-09-01 10:40:30      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

/// <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();
        }
    }
}

 

C# XML序列化

标签:

原文地址:http://www.cnblogs.com/taiyonghai/p/5828539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!