C#根据XML递归生成3位一级无限菜单

<s i="6255000000000000" n="Vsat公用事业" t="1" c="0" g="0" e="Utilities">
        <s i="6255100000000000" n="Vsat公用事业Ⅱ" t="1" c="0" g="0" e="Utilities Ⅱ">
          <s i="6255101000000000" n="Vsat电力Ⅲ" t="1" c="0" g="0" e="Electric Power Ⅲ">
            <s i="6255101010000000" n="Vsat电力" t="1" c="1" g="0" e="Electricity" />
          </s>
          <s i="6255102000000000" n="Vsat燃气Ⅲ" t="1" c="0" g="0" e="Gas Ⅲ">
            <s i="6255102010000000" n="Vsat燃气" t="1" c="1" g="0" e="Gas" />
          </s>
          <s i="6255103000000000" n="Vsat复合型公用事业Ⅲ" t="1" c="0" g="0" e="Composite Utilities Ⅲ">
            <s i="6255103010000000" n="Vsat复合型公用事业" t="1" c="1" g="0" e="Composite Utilities" />
          </s>
          <s i="6255104000000000" n="Vsat水务Ⅲ" t="1" c="0" g="0" e="Water Ⅲ">
            <s i="6255104010000000" n="Vsat水务" t="1" c="1" g="0" e="Water Utilities" />
          </s>
          <s i="6255105000000000" n="Vsat独立电力生产商与能源贸易商Ⅲ" t="1" c="0" g="0" e="Independent Power Producers &amp; Energy Traders Ⅲ">
            <s i="6255105010000000" n="Vsat独立电力生产商与能源贸易商" t="1" c="1" g="0" e="Independent Power Producers &amp; Energy Traders" />
            <s i="1000016335000000" n="Vsat新能源发电业者" t="1" c="0" g="0" e="New Energy Power Generation" />
          </s>
        </s>
      </s>

C#代码:

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

namespace ConsoleApplication4
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            StreamWriter sw = new StreamWriter(@"ConsoleOutput.txt");
            Console.SetOut(sw);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("SYSGRP.xml");
            XmlNode xn = xmlDoc.SelectSingleNode("s");
            GetElementList(xn, "001036");
            sw.Flush();
            sw.Close();
        }
        static int i = 1;
        static Dictionary<string, string> DicList = new Dictionary<string, string>();
        static int Num = 0;
        private static void GetElementList(XmlNode element, String id)
        {
            foreach (XmlNode node in element)
            {
                if (node.ParentNode.ParentNode.Attributes == null)
                {
                    if (DicList.ContainsKey(node.ParentNode.Attributes["i"].Value) == false)
                    {
                        DicList.Add(node.ParentNode.Attributes["i"].Value, id);
                    }
                }
                if (DicList.ContainsKey(node.Attributes["i"].Value) == false)
                {
                    for (int j = 0; j < node.ParentNode.ChildNodes.Count; j++)
                    {
                        if (node.ParentNode.ChildNodes.Item(j).Attributes["i"].Value == node.Attributes["i"].Value)
                        {
                            DicList.Add(node.Attributes["i"].Value, DicList[node.ParentNode.Attributes["i"].Value].ToString() + (j + 1).ToString().PadLeft(3, '0'));
                        }
                    }
                }

                if (DicList.ContainsKey(node.Attributes["i"].Value) == true)
                {
                    Console.WriteLine(node.Attributes["i"].Value + ":" + node.Attributes["n"].Value + ":" + DicList[node.Attributes["i"].Value].ToString());
                }

                if (node.HasChildNodes)
                {
                    GetElementList(node, id);
                }
            }
        }
    }

}

结果:

 

6255100000000000:Vsat公用事业Ⅱ:001036001
6255101000000000:Vsat电力Ⅲ:001036001001
6255101010000000:Vsat电力:001036001001001
6255102000000000:Vsat燃气Ⅲ:001036001002
6255102010000000:Vsat燃气:001036001002001
6255103000000000:Vsat复合型公用事业Ⅲ:001036001003
6255103010000000:Vsat复合型公用事业:001036001003001
6255104000000000:Vsat水务Ⅲ:001036001004
6255104010000000:Vsat水务:001036001004001
6255105000000000:Vsat独立电力生产商与能源贸易商Ⅲ:001036001005
6255105010000000:Vsat独立电力生产商与能源贸易商:001036001005001
1000016335000000:Vsat新能源发电业者:001036001005002

您可以选择一种方式赞助本站