using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml.Serialization; namespace TEAMModelOS.SDK.Helper.Common.XmlHelper { /// /// XML序列化公共处理类 /// public static class XmlSerializeHelper { /// /// 将实体对象转换成XML /// /// 实体类型 /// 实体对象 public static string XmlSerialize(T obj) { try { using (StringWriter sw = new StringWriter()) { Type t = obj.GetType(); XmlSerializer serializer = new XmlSerializer(obj.GetType()); serializer.Serialize(sw, obj); sw.Close(); return sw.ToString(); } } catch (Exception ex) { throw new Exception("将实体对象转换成XML异常", ex); } } /// /// 将XML转换成实体对象 /// /// 实体类型 /// XML public static T DESerializer(string strXML) where T : class { try { using (StringReader sr = new StringReader(strXML)) { XmlSerializer serializer = new XmlSerializer(typeof(T)); return serializer.Deserialize(sr) as T; } } catch (Exception ex) { throw new Exception("将XML转换成实体对象异常", ex); } } } }