C# - XML的目录结构

时间:2018-04-27 09:33:15

标签: c# xml subdirectory

使用C#我有一个包含文件夹结构的XML文件,用户选择主项目文件夹,然后系统根据XML文件的结构开始检查主项目文件夹中的子文件夹。文件夹结构是动态的,因为应用程序的管理员可以通过更改XML文件来添加/删除/修改结构中的文件夹。

如何遍历XML,我尝试使用XmlDocument获取完整的目录路径但是我读到了使用XDocument以获得更好的结果,但我对XML的了解仍然是基本的。

XML文件结构是:

yourSvg = document.querySelector("svg");
    yourCircle = svg.querySelector("circle");
    yourATag = svg.querySelector("a");
    yourATag.appendChild(yourCircle)

编辑 - 1

我尝试使用此测试代码来获取节点和子节点的路径

<?xml version="1.0" encoding="utf-8" ?>

<dir name="Site Documents">
  <dir name="External">
    <dir name="Mechanical">
      <dir name="01. Submittals">
        <dir name="1. Sent">
        </dir>
        <dir name="2. Received" />
      </dir>
      <dir name="02. Drawings">
        <dir name="1. Sent">
        </dir>
        <dir name="2. Received" />
      </dir>
      <dir name="03. MIR">
        <dir name="1. Sent">
        </dir>
        <dir name="2. Received" />
      </dir>
      <dir name="04. IR">
        <dir name="1. Sent">
        </dir>
        <dir name="2. Received" />
      </dir>
    </dir>
    <dir name="Electrical">
      <dir name="01. Submittals">
        <dir name="1. Sent">
        </dir>
        <dir name="2. Received" />
      </dir>
      <dir name="02. Drawings">
        <dir name="1. Sent">
        </dir>
        <dir name="2. Received" />
      </dir>
      <dir name="03. MIR">
        <dir name="1. Sent">
        </dir>
        <dir name="2. Received" />
      </dir>
      <dir name="04. IR">
        <dir name="1. Sent">
        </dir>
        <dir name="2. Received" />
      </dir>
    </dir>
  </dir>
  <dir name="Internal">
    <dir name="01. PR">
      <dir name="1. MECH">
      </dir>
      <dir name="2. ELEC" />
    </dir>
    <dir name="02. PO">
    </dir>
    <dir name="03. SRF">
    </dir>
    <dir name="04. RMR" />
  </dir>
</dir>

文本框的输出是:

private void button2_Click(object sender, EventArgs e) { XmlDocument xDoc = new XmlDocument(); xDoc.Load(@"C:\Users\John_Doe\_data\directory_hirarchy.xml"); XmlNodeList xmlFolderName = xDoc.SelectNodes("//dir"); MessageBox.Show(xmlFolderName.Count.ToString()); string finalText = ""; for (int ctr = 0; ctr < xmlFolderName.Count; ctr++) { string DocFolder = xmlFolderName[ctr].Attributes["name"].InnerText; finalText = finalText + DocFolder + "\r\n"; } txtDisplay.Text = finalText; // Test text box for Output Result }

由Daisy Shipton解决

Site Documents
External
Mechanical
01. Submittals
1. Sent
2. Received
02. Drawings
1. Sent
2. Received
03. MIR
1. Sent
2. Received
04. IR
1. Sent
2. Received
Electrical
01. Submittals
1. Sent
2. Received
02. Drawings
1. Sent
2. Received
03. MIR
1. Sent
2. Received
04. IR
1. Sent
2. Received
Internal
01. PR
1. MECH
2. ELEC
02. PO
03. SRF
04. RMR

1 个答案:

答案 0 :(得分:3)

这里有两个选项:

  • 使用递归,这样您就可以跟踪到目前为止的路径&#34;
  • 只需看看所有元素,但使用他们的祖先来构建路径

第一个可能最容易理解,是的,使用LINQ to XML可以简化生活。

using System;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        var doc = XDocument.Load("test.xml");
        PrintDirectories(doc, null);        
    }

    static void PrintDirectories(XContainer parent, string path)
    {
        foreach (XElement element in parent.Elements("dir"))
        {
            string dir = element.Attribute("name").Value;
            string fullPath = path == null ? dir : $"{path}/{dir}";
            Console.WriteLine(fullPath);
            PrintDirectories(element, fullPath);
        }
    }
}

非递归方法大小相同,但如果您不熟悉LINQ,可能更难理解:

using System;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        var doc = XDocument.Load("test.xml");
        var directories = doc.Descendants("dir");

        foreach (var dir in directories)
        {
            var parts = dir
                .AncestorsAndSelf() // All the ancestors of this element, and itself
                .Reverse()          // Reversed (so back into document order)
                .Select(e => e.Attribute("name").Value); // Select the name
            var path = string.Join("/", parts);
            Console.WriteLine(path);
        }
    }   
}