C# XML序列化
Posted 小台的IT备忘录
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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序列化的主要内容,如果未能解决你的问题,请参考以下文章