using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Web.Script.Serialization; using System.Xml; using System.Xml.Serialization; namespace Com.AppCode.Helper { /// <summary> /// 泛型序列化 /// </summary> public class XmlHelper { /// <summary> /// 对象序列化成 XML String /// </summary> public static string Serialize<T>(T obj) { string xmlString = string.Empty; XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream()) { xmlSerializer.Serialize(ms, obj); xmlString = Encoding.UTF8.GetString(ms.ToArray()); } return xmlString; } /// <summary> /// XML String 反序列化成对象 /// </summary> public static T Deserialize<T>(string xmlString) { T t = default(T); XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString))) { using (XmlReader xmlReader = XmlReader.Create(xmlStream)) { Object obj = xmlSerializer.Deserialize(xmlReader); t = (T)obj; } } return t; } /// <summary> /// XML String 反序列化成对象 /// </summary> public static List<T> DeserializeToList<T>(string xml) { return new List<T>(); } /* Test var xml = @"<classes> <class> <classname>高三一班</classname> <id>1</id> <remark>学霸</remark> <students> <student> <name>爱因斯坦</name> <sex>男</sex> <age>18</age> </student> <student> <name>牛顿</name> <sex>男</sex> <age>18</age> </student> </students> </class> <class> <classname>高三Ⅹ班</classname> <id>1</id> <remark>渣渣</remark> <students> <student> <name>特朗普</name> <sex>男</sex> <age>18</age> </student> <student> <name>本</name> <sex>男</sex> <age>18</age> </student> </students> </class> </classes>"; List<ClassSet> list = XmlHelper.XmlToList<ClassSet>(xml, "classes"); [Serializable, XmlType("class")] public class ClassSet { [XmlElement("classname")] public string Name { get; set; } [XmlElement("id")] public int Id { get; set; } [XmlElement("remark")] public string Remark { get; set; } [XmlArray("students")] public List<Student> Students { get; set; } [Serializable, XmlType("student")] public class Student { [XmlElement("name")] public string Name { get; set; } [XmlElement("sex")] public string Sex { get; set; } [XmlElement("age")] public int Age { get; set; } } } */ public static List<T> XmlToList<T>(string xml, string rootName) where T : class { XmlSerializer serializer = new XmlSerializer(typeof(List<T>), new XmlRootAttribute(rootName)); using (StringReader sr = new StringReader(xml)) { List<T> list = serializer.Deserialize(sr) as List<T>; return list; } } } /// <summary> /// <remarks>Xml序列化与反序列化</remarks> /// <creator>zhangdapeng</creator> /// </summary> public class XmlSerializeUtil { #region 反序列化 /// <summary> /// 反序列化 /// </summary> /// <param name="type">类型</param> /// <param name="xml">XML字符串</param> /// <returns></returns> public static object Deserialize(Type type, string xml) { using (StringReader sr = new StringReader(xml)) { XmlSerializer xmldes = new XmlSerializer(type); return xmldes.Deserialize(sr); } } /// <summary> /// 反序列化 /// </summary> /// <param name="type"></param> /// <param name="xml"></param> /// <returns></returns> public static object Deserialize(Type type, Stream stream) { XmlSerializer xmldes = new XmlSerializer(type); return xmldes.Deserialize(stream); } #endregion #region 序列化 /// <summary> /// 序列化 /// </summary> /// <param name="type">类型</param> /// <param name="obj">对象</param> /// <returns></returns> public static string Serializer(Type type, object obj) { MemoryStream Stream = new MemoryStream(); XmlSerializer xml = new XmlSerializer(type); //序列化对象 xml.Serialize(Stream, obj); Stream.Position = 0; StreamReader sr = new StreamReader(Stream); string str = sr.ReadToEnd(); sr.Dispose(); Stream.Dispose(); return str; } #endregion } }