XML文件的读取、序列化和反序列化操作

    public class XmlHelper
    {
        //从xml中获取MsgType
        public static string XMLSelect(string XML)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(XML);
            XmlNodeList Msgxnl = xmldoc.SelectNodes("xml/MsgType");
            XmlNodeList Eventxnl = xmldoc.SelectNodes("xml/Event");
            string MsgType = string.Empty;
            string Event = string.Empty;
            if (Msgxnl.Count != 0)//msgType不为空
            {
                MsgType = Msgxnl[0].InnerText.Trim();
                if (MsgType == "event")
                {
                    if (Eventxnl.Count != 0)//event不为空
                    {
                        Event = Eventxnl[0].InnerText.Trim();
                        if (Event == "unsubscribe" || Event == "subscribe" || Event == "LOCATION")
                        {
                            MsgType = Event;
                        }
                        else//eventKey存在
                        {
                            XmlNodeList xnlEventKey = xmldoc.SelectNodes("xml/EventKey");
                            if (xnlEventKey.Count != 0)
                            {
                                MsgType = xnlEventKey[0].InnerText.Trim();
                            }
                        }
                    }
                }
            }
            //else
            //{
            //    XmlNode xn = xmldoc.SelectSingleNode("UpdateInfo/MsgType");
            //    if (xn!=null)
            //    {
            //        MsgType = xn.InnerText.Trim();
            //    }
            //}
            return MsgType;
        }
        //对象反序列化
        public static T XmlDeserialize<T>(string xmlString)
        {
            T t = default(T);
            using (MemoryStream stream = new MemoryStream())
            {
                using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8))
                {
                    sw.Write(xmlString);
                    sw.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    try
                    {
                        t = ((T)serializer.Deserialize(stream));
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            return t;
        }
        //自定义对象序列化成字符串
        public static string CustomXMLSerialize<T>(T obj, string XML)
        {
            StringBuilder stringBuilder = new StringBuilder();
            Type type = obj.GetType();//TextSendInfo
            object[] classAtts = obj.GetType().GetCustomAttributes(typeof(XmlRootAttribute), false);//返回自定义特性的数组,XmlRootAttribute
            if (classAtts.Length > 0)
            {
                string XmlRoot = ((XmlRootAttribute)classAtts[0]).ElementName;//xml
                stringBuilder.Append("<");
                stringBuilder.Append(XmlRoot);
                stringBuilder.Append(">");

                PropertyInfo[] propertyArr = obj.GetType().GetProperties();//5个属性元素
                if (propertyArr.Length > 0)
                {
                    foreach (var propertyInfo in propertyArr)
                    {
                        object value = propertyInfo.GetValue(obj, null);//根据propertyInfo属性从obj中获取值,"请先在授权页面申请授权,再使用该功能!"
                        object[] propertyAtts = propertyInfo.GetCustomAttributes(typeof(XmlElementAttribute), false);//XmlElementAttribute
                        if (propertyAtts.Length > 0)
                        {
                            string xmlElementName = ((XmlElementAttribute)propertyAtts[0]).ElementName;//content
                            stringBuilder.Append("<");
                            stringBuilder.Append(xmlElementName);
                            stringBuilder.Append(">");
                            if (xmlElementName == "CreateTime")
                            {
                                stringBuilder.Append(value);
                            }
                            else
                            {
                                stringBuilder.Append("<![CDATA[" + value + "]]>");
                            }
                            stringBuilder.Append("</");
                            stringBuilder.Append(xmlElementName);
                            stringBuilder.Append(">");
                        }
                    }
                }
                if (XML != string.Empty)
                {
                    stringBuilder.Append(XML);
                }
                stringBuilder.Append("</");
                stringBuilder.Append(XmlRoot);
                stringBuilder.Append(">");
            }
            return stringBuilder.ToString();
        }
    }
原文地址:https://www.cnblogs.com/slu182/p/4252685.html