首页 > 代码库 > C# XML序列化

C# XML序列化

/// <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序列化