XML管理类

前端之家收集整理的这篇文章主要介绍了XML管理类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

用于快速生成实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace XMLControl
{
    /// <summary>
    /// XML管理类
    /// </summary>
    public class XMLManager
    {
        public XMLManager()
        {


        }

        public static StringBuilder CreateModel(string xml)
        {
            StringBuilder sbuilder = new StringBuilder();

            XmlDocument document = new XmlDocument();
            document.LoadXml(xml);

            try
            {
                XmlElement ele = document.DocumentElement;
                List<StringBuilder> lstClass = new List<StringBuilder>();
                NewMethodXml(lstClass,ele);
                if (lstClass.Count > 0)
                {
                    StringBuilder sBuilderInit = new StringBuilder();
                    sBuilderInit.AppendLine("}");
                    lstClass.Add(sBuilderInit);
                }
                foreach (StringBuilder item in lstClass)
                {
                    sbuilder.Append(item);
                    sbuilder.AppendLine();
                }
            }
            catch (Exception ex)
            {

            }

            return sbuilder;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static void NewMethodXml(List<StringBuilder> lstClass,XmlNode ele)
        {
            if (lstClass == null)
                lstClass = new List<StringBuilder>();
            XmlNodeList lstXmlNode = ele.ChildNodes;
            if (lstXmlNode.Count > 0)
            {
                if (lstClass.Count == 0)
                {
                    StringBuilder sBuilderInit = new StringBuilder();
                    sBuilderInit.AppendLine("using System;");
                    sBuilderInit.AppendLine("using System.Collections.Generic;");
                    sBuilderInit.AppendLine("namespace 命名空间");
                    sBuilderInit.AppendLine("{");

                    lstClass.Add(sBuilderInit);
                }
                StringBuilder sBuilder = new StringBuilder();
                lstClass.Add(sBuilder);

                sBuilder.AppendLine("     [Serializable]");
                sBuilder.AppendFormat("     public partial class {0}",ele.Name);
                sBuilder.AppendLine();
                sBuilder.AppendLine("     {");
                foreach (XmlNode item in lstXmlNode)
                {
                    XmlNodeList lst = ele.SelectNodes(item.Name);
                    if (lst.Count > 1)
                    {
                        string ww = string.Format("          public List<{0}> {1}",item.Name,ele.Name);
                        if (sBuilder.ToString().Contains(ww))
                            continue;
                        sBuilder.AppendFormat("          [System.Xml.Serialization.XmlArrayItemAttribute(\"{0}\",IsNullable = false)]",item.Name);
                        sBuilder.AppendLine();
                        sBuilder.AppendFormat(ww);
                        sBuilder.Append(" { set; get; }");
                        sBuilder.AppendLine();
                        NewMethodAttribute(lstClass,item);
                    }
                    else
                    {
                        if (ele.Name == "feeitemarray")
                        {

                        }
                        XmlNodeList lst1 = null;
                        if (item.FirstChild != null && !(item.FirstChild is XmlText))
                            lst1 = item.SelectNodes(item.FirstChild.Name);
                        if (item.ChildNodes.Count > 1)
                        {
                            if (lst1 != null && lst1.Count > 1)
                            {
                                string ww = string.Format("          public List<{0}> {1}",item.FirstChild.Name,item.Name);
                                if (sBuilder.ToString().Contains(ww))
                                    continue;
                                sBuilder.AppendFormat("          [System.Xml.Serialization.XmlArrayItemAttribute(\"{0}\",item.FirstChild.Name);
                                sBuilder.AppendLine();
                                sBuilder.AppendFormat(ww);
                                sBuilder.Append(" { set; get; }");
                                sBuilder.AppendLine();
                                NewMethodAttribute(lstClass,item.FirstChild);
                            }
                            else
                            {
                                sBuilder.AppendFormat("          public {0} {0}",item.Name);
                                sBuilder.Append(" { set; get; }");
                                sBuilder.AppendLine();
                                NewMethodXml(lstClass,item);
                            }
                        }
                        else
                        {
                            sBuilder.AppendFormat("          public string {0}",item.Name);
                            sBuilder.Append(" { set; get; }");
                            sBuilder.AppendLine();
                        }
                    }
                }

                sBuilder.AppendLine("     }");
            }
        }
        /// <summary>
        /// 生成类(节点属性)
        /// </summary>
        /// <param name="lstClass"></param>
        /// <param name="ele"></param>
        public static void NewMethodAttribute(List<StringBuilder> lstClass,XmlNode ele)
        {
            StringBuilder sBuilder = new StringBuilder();
            lstClass.Add(sBuilder);

            sBuilder.AppendLine("     [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]");
            sBuilder.AppendFormat("     public class {0}",ele.Name);
            sBuilder.AppendLine();
            sBuilder.AppendLine("     {");
            foreach (XmlAttribute item in ele.Attributes)
            {
                sBuilder.AppendLine("          [System.Xml.Serialization.XmlAttributeAttribute()]");
                sBuilder.AppendFormat("          public string {0}",item.Name);
                sBuilder.Append(" { set; get; }");
                sBuilder.AppendLine();
            }
            sBuilder.AppendLine("     }");
        }

        private static void NewMethod(StringBuilder sbuilder,XmlNode ele)
        {
            XmlNodeList lstXmlNode = ele.ChildNodes;
            foreach (XmlNode item in lstXmlNode)
            {
                XmlNodeList lstSubXmlNode = item.SelectNodes(item.Name);
                if (lstSubXmlNode.Count == 0)
                {
                    //NewMethod(sbuilder,item);
                    XmlNodeList lstSubNode = item.ChildNodes;
                    XmlNodeList lstSubNode2 = item.ParentNode.SelectNodes(item.Name);
                    if (lstSubNode.Count > 1 || lstSubNode2.Count > 1)
                    {
                        sbuilder.AppendLine(item.Name + "   =>0 类");
                        foreach (XmlNode info in lstSubNode)
                        {
                            NewMethod(sbuilder,info);
                        }
                    }
                    else
                    {
                        sbuilder.AppendLine(item.Name + "=>属性");
                    }
                }
                else
                {
                    sbuilder.AppendLine(item.Name + "=>" + lstSubXmlNode.Count);
                }
            }
            if (lstXmlNode.Count == 0)
            {
                if (!(ele is XmlText))
                    sbuilder.AppendLine(ele.Name + "=>:0");
            }
        }
    }
}
原文链接:https://www.f2er.com/xml/294259.html

猜你在找的XML相关文章