• 彻底解析XML


     先是两个正规的解析方法

            #region 反序列化
            /// <summary>
            /// 反序列化
            /// </summary>
            /// <param name="type">类型</param>
            /// <param name="xml">XML字符串</param>
            /// <returns></returns>
            private 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)
                {
    
                    return null;
                }
            }
    
    
            /// <summary>
            /// 反序列化
            /// </summary>
            /// <param name="type"></param>
            /// <param name="xml"></param>
            /// <returns></returns>
            private 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>
            private static string Serializer(Type type, object obj)
            {
                MemoryStream Stream = new MemoryStream();
                XmlSerializer xml = new XmlSerializer(type);
                try
                {
                    //序列化对象
                    xml.Serialize(Stream, obj);
                }
                catch (InvalidOperationException)
                {
                    throw;
                }
                Stream.Position = 0;
                StreamReader sr = new StreamReader(Stream);
                string str = sr.ReadToEnd();
    
                sr.Dispose();
                Stream.Dispose();
    
                return str;
            }
    
            #endregion

    自己感觉挺麻烦,自己整个感觉简单的

     1     /// <summary>
     2         /// 解析Xml,返回Ilist<string>类型
     3         /// </summary>
     4         /// <param name="stringXml">XML  string文档</param>
     5         /// <param name="TipName">根节点名称</param>
     6         /// <returns></returns>
     7         private static IList<string> XmlDoc(string stringXml, string TipName = "root")
     8         {
     9             IList<string> XmlList = new List<string>();
    10 
    11             XmlDocument xmlDoc = new XmlDocument();
    12             xmlDoc.LoadXml(stringXml);
    13 
    14             string firstNodeName = xmlDoc.DocumentElement.Name;
    15 
    16             XmlNode rootNode = xmlDoc.SelectSingleNode(firstNodeName);
    17 
    18 
    19             foreach (XmlNode xxNode in rootNode.ChildNodes)
    20             {
    21                 if (xxNode.FirstChild != null)
    22                 {
    23                     if (xxNode.FirstChild.LastChild != null)
    24                     {
    25                         XmlList = XmlDoc("<?xml version="1.0" encoding="utf-8"?>" + xxNode.OuterXml, TipName + "__" + xxNode.Name);
    26                     }
    27                     else
    28                     {
    29                         string dsf = xxNode.InnerText;
    30                         string sdf = TipName + "__" + xxNode.Name;
    31                         XmlList.Add(sdf);
    32                         XmlList.Add(dsf);
    33                     }
    34                 }
    35                 else
    36                 {
    37                     string dsf = xxNode.InnerText;
    38                     string sdf = TipName + "__" + xxNode.Name;
    39                     XmlList.Add(sdf);
    40                     XmlList.Add(dsf);
    41                 }
    42             }
    43 
    44             return XmlList;
    45         }
    46 
    47 
    48         /// <summary>
    49         /// 返回字典类型,将解析的集合进行整理
    50         /// </summary>
    51         /// <param name="stringXml">XML  string文档</param>
    52         /// <returns></returns>
    53         public static IDictionary<string, List<string>> XmlDoc(string stringXml)
    54         {
    55             IList<string> XmlList = new List<string>();
    56 
    57             XmlDocument xmlDoc = new XmlDocument();
    58             xmlDoc.LoadXml(stringXml);
    59 
    60             string TipName = xmlDoc.DocumentElement.Name;
    61 
    62 
    63             IList<string> xmlDocCollect = XmlDoc(stringXml, TipName);
    64 
    65             IDictionary<string, List<string>> xmlElementCollect = new Dictionary<string, List<string>>();
    66 
    67             for (int i = 0; i < xmlDocCollect.Count(); i++)
    68             {
    69                 if (xmlElementCollect.Keys.Contains(xmlDocCollect[i]))
    70                 {
    71                     xmlElementCollect[xmlDocCollect[i]].Add(xmlDocCollect[i + 1]);
    72                 }
    73                 else
    74                 {
    75                     xmlElementCollect.Add(xmlDocCollect[i], new List<string>() { xmlDocCollect[i + 1] });
    76                 }
    77                 i = i + 1;
    78             }
    79             return xmlElementCollect;
    80         }

    记录一下,省的找不到

  • 相关阅读:
    别让你的生活止于平庸!(摘)
    NSURLSession 请求
    第三方原理
    iOS实用的小技巧
    简书APP
    网络请求
    JQuery 简介
    struts2拦截器的实现原理及源码剖析
    hibernate配置文件注意点
    hibernate中三种状态
  • 原文地址:https://www.cnblogs.com/cwmizlp/p/10064385.html
Copyright © 2020-2023  润新知