using System; using System.IO; using System.Xml.Serialization; namespace RS.Common { /// /// Xml序列化与反序列化 /// public class XSerializer { #region 反序列化 /// /// 反序列化 /// /// 类型 /// XML字符串 /// public static object Deserialize(Type type, string xml) { try { using (StringReader sr = new StringReader(xml)) { XmlSerializer xmldes = new XmlSerializer(type); return xmldes.Deserialize(sr); } } catch (Exception e) { // 返回异常 throw new Exception(e.Message.ToString()); } } /// /// 数据流反序列化 /// /// /// /// public static object Deserialize(Type type, Stream stream) { XmlSerializer xmldes = new XmlSerializer(type); return xmldes.Deserialize(stream); } #endregion 反序列化 #region 序列化 /// /// 序列化 /// /// 类型 /// 对象 /// public static string Serializer(T t) { using (StringWriter sw = new StringWriter()) { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer xz = new XmlSerializer(t.GetType()); xz.Serialize(sw, t, ns); return sw.ToString().Replace("\r\n", "").Replace("\r\n", "").Replace(" ", ""); } } #endregion 序列化 } }