将具有相同元素的嵌套XML转换为Class

时间:2017-02-07 20:12:10

标签: c# xml xml-deserialization

需要一些帮助。 我有一个XML,其中相同的元素彼此嵌套。 我能够使用Convert XML String to Object问题创建一个C#类,在类中粘贴特殊的XML。 问题是代码不断为每个嵌套元素创建类。 我只想为所有子元素设置一个泛型类。

以下是XML示例:

 <?xml version="1.0" encoding="utf-8" ?>
<CompanyFolders>
  <company title="My Company">
    <departments>
      <department title="Retail Operations">
        <folders>
          <folder tite="Model Store"/>
          <folder tite="Retail Ops Manual"/>
          <folder tite="VM Guides &amp; planograms"/>
          <folder tite="About our Team"/>
        </folders>
      </department>
      <department title="Buying">
        <folders>
          <folder tite="Gold Jewellery">
            <folders>
              <folder tite="Gold">
                <folders>
                  <folder title="Color Range"/>
                  <folder title="Launch Packs"/>
                  <folder title="Grading"/>
                  <folder title="Herolines"/>
                  <folder title="Top Sellers"/>
                  <folder title="Launch &amp; price Change Calendar"/>
                </folders>
              </folder>
              <folder tite="Branded">
                <folders>
                  <folder title="Color Range"/>
                  <folder title="Launch Packs"/>
                  <folder title="Grading"/>
                  <folder title="Herolines"/>
                  <folder title="Top Sellers"/>
                  <folder title="Launch &amp; price Change Calendar"/>
                </folders>
              </folder>
            </folders>    
          </folder>
          <folder tite="Rings">
            <folders>
              <folder tite="Monthly Sales by store by diamond brand" />
              <folder tite="Line lists &amp; core ranges" />
              <folder tite="Current Hero Lines" />
              <folder tite="Diamond ambassador conference call details and agenda" />
            </folders>    
          </folder>
        </folders>
      </department>
      <department title="Property">
        <folders>
          <folder tite="Maintenance Forms"/>
          <folder tite="Portal for reporting maintenance issues"/>
          <folder tite="Security Advice"/>
          <folder tite="Security Policy"/>
          <folder tite="H&amp;S Policy and awareness"/>
          <folder tite="H&amp;S Forms"/>
          <folder tite="Insurance Documents"/>
          <folder tite="Out of Emergencies Contact list"/>
        </folders>
      </department>
    </departments>
  </company>
</CompanyFolders>

课程创建:

namespace MyXML
{

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class CompanyFolders
    {

        private CompanyFoldersCompany[] companyField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("company")]
        public CompanyFoldersCompany[] company
        {
            get
            {
                return this.companyField;
            }
            set
            {
                this.companyField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CompanyFoldersCompany
    {

        private CompanyFoldersCompanyDepartment[] departmentsField;

        private string titleField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("department", IsNullable = false)]
        public CompanyFoldersCompanyDepartment[] departments
        {
            get
            {
                return this.departmentsField;
            }
            set
            {
                this.departmentsField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CompanyFoldersCompanyDepartment
    {

        private CompanyFoldersCompanyDepartmentFolder[] foldersField;

        private string titleField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("folder", IsNullable = false)]
        public CompanyFoldersCompanyDepartmentFolder[] folders
        {
            get
            {
                return this.foldersField;
            }
            set
            {
                this.foldersField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CompanyFoldersCompanyDepartmentFolder
    {

        private CompanyFoldersCompanyDepartmentFolderFolder[] foldersField;

        private string titeField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("folder", IsNullable = false)]
        public CompanyFoldersCompanyDepartmentFolderFolder[] folders
        {
            get
            {
                return this.foldersField;
            }
            set
            {
                this.foldersField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string tite
        {
            get
            {
                return this.titeField;
            }
            set
            {
                this.titeField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CompanyFoldersCompanyDepartmentFolderFolder
    {

        private CompanyFoldersCompanyDepartmentFolderFolderFolder[] foldersField;

        private string titeField;

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("folder", IsNullable = false)]
        public CompanyFoldersCompanyDepartmentFolderFolderFolder[] folders
        {
            get
            {
                return this.foldersField;
            }
            set
            {
                this.foldersField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string tite
        {
            get
            {
                return this.titeField;
            }
            set
            {
                this.titeField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public partial class CompanyFoldersCompanyDepartmentFolderFolderFolder
    {

        private string titleField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
            }
        }
    }
}

所以你可以注意到为内部文件夹创建了CompanyFoldersCompanyDepartmentFolderFolder和CompanyFoldersCompanyDepartmentFolderFolderFolder,但是我只想要一种递归的CompanyFoldersCompanyDepartmentFolder。

我很感激帮助。

1 个答案:

答案 0 :(得分:0)

请尝试以下代码。你应该学会拼写标题(不是tite)。

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement companyFolders = (XElement)doc.FirstNode;
            new CompanyFolders(companyFolders);
        }
    }
    public class CompanyFolders
    {
        public static List<Company> companies = new List<Company>();
        public CompanyFolders(XElement companies)
        {
            CompanyFolders.companies = companies.Elements("company").Select(x => new Company(x)).ToList();
        }

        public class Company
        {
            string title { get; set; }
            public List<Department> departments = new List<Department>();
            public Company(XElement company)
            {
                title = (string)company.Attribute("title");
                departments = company.Descendants("department").Select(x => new Department(x)).ToList();
            }
            public class Department
            {
                string title { get; set; }
                public List<Folder> folders = new List<Folder>();
                public Department(XElement department)
                {
                    title = (string)department.Attribute("title");
                    folders = department.Element("folders").Elements("folder").Select(x => new Folder(x)).ToList();
                }
                public class Folder
                {
                    string title { get; set; }
                    public List<Folder> folders = new List<Folder>();
                    public Folder(XElement folder)
                    {
                        title = (string)folder.Attribute("title");
                        if (folder.Element("folders") != null)
                        {
                            folders = folder.Element("folders").Elements("folder").Select(x => new Folder(x)).ToList();
                        }
                    }
                }
            }
        }
    }
}