首页 > 代码库 > XML序列化和反序列化 以及相关类的写法

XML序列化和反序列化 以及相关类的写法

类的写法:

省网办数据对接中运用到

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml.Serialization;namespace SWBExchange.Common.Entities{    public class Body    {        [XmlElement(ElementName = "BasicInfo")]        public BasicInfo BasicInfo { get; set; }        [XmlElement(ElementName = "UserInfos")]        public UserInfos UserInfos { get; set; }        [XmlElement(ElementName = "OrgInfos")]        public OrgInfos OrgInfos { get; set; }        [XmlElement(ElementName = "ServiceInfo")]        public ServiceInfo ServiceInfo { get; set; }        [XmlElement(ElementName = "ApasInfo")]        public ApasInfo ApasInfo { get; set; }        [XmlElement(ElementName = "Review")]        public Review Review { get; set; }        [XmlElement(ElementName = "Node")]        public Node Node { get; set; }        [XmlElement(ElementName = "Proposer")]        public Proposer Proposer { get; set; }        [XmlElement(ElementName = "Operator")]        public Operator Operator { get; set; }        [XmlArray("Expresses"), XmlArrayItem("Express")]        public List<Express> Expresses { get; set; }        [XmlArray("FormInfos"), XmlArrayItem("FormInfo")]        public List<FormInfo> FormInfos { get; set; }        [XmlArray("Attrs"), XmlArrayItem("Attr")]        public List<Attr> Attrs { get; set; }        [XmlArray("Licenses"), XmlArrayItem("License")]        public List<License> Licenses { get; set; }        [XmlElement(ElementName = "SMS")]        public SMS SMS { get; set; }    }}
    public class Helper    {        #region 反序列化        public static string ObjectToXML<T>(T t)        {            return ObjectToXML<T>(t, Encoding.UTF8);        }        /// <summary>        /// 将object对象序列化成XML        /// </summary>        /// <param name="o"></param>        /// <returns></returns>        public static string ObjectToXML<T>(T t, Encoding encoding)        {            XmlSerializer ser = new XmlSerializer(t.GetType());            Encoding utf8EncodingWithNoByteOrderMark = new UTF8Encoding(false);            using (MemoryStream mem = new MemoryStream())            {                using (XmlTextWriter writer = new XmlTextWriter(mem, utf8EncodingWithNoByteOrderMark))                {                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();                    ns.Add("", "");                    ser.Serialize(writer, t, ns);                    return encoding.GetString(mem.ToArray());                }            }        }        #endregion        #region 序列化        public static T XMLToObject<T>(string source)        {            return XMLToObject<T>(source, Encoding.UTF8);        }        public static T XMLToObject<T>(string source, Encoding encoding)        {            XmlSerializer mySerializer = new XmlSerializer(typeof(T));            using (MemoryStream stream = new MemoryStream(encoding.GetBytes(source)))            {                return (T)mySerializer.Deserialize(stream);            }        }        #endregion

  

XML序列化和反序列化 以及相关类的写法