简体   繁体   中英

C# Create XML with unlimited child nodes based on number property

I have the following C# class in my project:

public class Department
{
   public string Name { get; set; }

   public string Number{ get; set; }

   public List<Department> SubDepartments{ get; set; }
}

A department can have sub-departments that have sub-departments on their own. The Number property determines the parent-child relationship and there is no limit for the depth.

I have a feed with a collection of departments that have only Name and Number and no SubDepartments . Which is the best way to create a XML file with child nodes for sub-departments based on the Number property?

Example:

<MainDepartment name="IT" number="1">
    <SubDepartment name="Software engineering" number="1.1" >
        <SubDepartment name="Quality assurance" number="1.1.1">
            ...
        </SubDepartment>
        <SubDepartment name="Development" number="1.1.2" />
    </SubDepartment>
    <SubDepartment name="Support" number="1.2" />
    <SubDepartment name="Administration" number="1.3" />
</MainDepartment>
<MainDepartment name="Finance" number="2" >
    <SubDepartment name="Accounting" number="2.1">
        ...
    </SubDepartment>
</MainDepartment>

Try following:

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)
        {
            List<Department> departments = new List<Department>() {
                new Department() { Name = "IT", Number = "1", SubDepartments = new List<Department>() {
                    new Department() { Name = "Software engineering", Number = "1.1", SubDepartments = new List<Department>() {
                        new Department() { Name = "Quality assurnace", Number = "1.1.1"},
                        new Department() { Name = "Development", Number = "1.1.2"}
                    }},
                    new Department() { Name = "Support", Number = "1.2"},
                    new Department() { Name = "Administration", Number = "1.3"}
               }},
               new Department() { Name = "Finance", Number = "2", SubDepartments = new List<Department>() {
                   new Department() { Name = "Accounting", Number = "2.1"}
               }}
            };

            string header = "<Company></Company>";
            XDocument doc = XDocument.Parse(header);
            XElement company = doc.Root;
            int level = 1;
            CreateXml(departments, company, level);
            doc.Save(FILENAME);

        }
        static void CreateXml(List<Department> parentDept, XElement parentXml, int level)
        {
            foreach (Department department in parentDept)
            {
                XElement newDept;
                if (level == 1)
                {
                    newDept = new XElement("MainDepartment");
                }
                else
                {
                    newDept = new XElement("SubDepartment");
                }
                parentXml.Add(newDept);
                newDept.Add(new XAttribute("name", department.Name));
                newDept.Add(new XAttribute("number", department.Number));
                if (department.SubDepartments != null)
                {
                    CreateXml(department.SubDepartments, newDept, level + 1);
                }
            }
        }
    }
    public class Department
    {
        public string Name { get; set; }

        public string Number { get; set; }

        public List<Department> SubDepartments { get; set; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM