在 C# 中创建表示文件夹结构(包括子文件夹)的 XML 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15096397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:02:18  来源:igfitidea点击:

Creating XML file representing folder structure (including subfolders) in C#

c#xmldirectory

提问by Deepanjan Nag

How can produce an XML file structuring a given folder to recursively represent all the files & subfolders within it?

如何生成构造给定文件夹的 XML 文件以递归表示其中的所有文件和子文件夹?

采纳答案by MarcinJuraszek

That's great example of problem, that can be easily solved using recursive algorithm!

这是一个很好的问题示例,可以使用递归算法轻松解决!

Pseudo-code:

伪代码:

function GetDirectoryXml(path)
    xml := "<dir name='" + path + "'>"

    dirInfo := GetDirectoryInfo(path)
    for each file in dirInfo.Files
        xml += "<file name='" + file.Name + "' />"
    end for

    for each subDir in dirInfo.Directories
        xml += GetDirectoryXml(subDir.Path)
    end for

    xml += "</dir>"

    return xml
end function

It can be done with C# and DirectoryInfo/XDocument/XElementclasses like that:

它可以用C#和做DirectoryInfo/ XDocument/XElement这样的类:

public static XElement GetDirectoryXml(DirectoryInfo dir)
{
    var info = new XElement("dir",
                   new XAttribute("name", dir.Name));

    foreach (var file in dir.GetFiles())
        info.Add(new XElement("file",
                     new XAttribute("name", file.Name)));

    foreach (var subDir in dir.GetDirectories())
        info.Add(GetDirectoryXml(subDir));

    return info;
}

And example of usage:

和用法示例:

static void Main(string[] args)
{
    string rootPath = Console.ReadLine();
    var dir = new DirectoryInfo(rootPath);

    var doc = new XDocument(GetDirectoryXml(dir));

    Console.WriteLine(doc.ToString());

    Console.Read();
}

Output for one of directories on my laptop:

我的笔记本电脑上的目录之一的输出:

<dir name="eBooks">
  <file name="Edulinq.pdf" />
  <file name="MCTS 70-516 Accessing Data with Microsoft NET Framework 4.pdf" />
  <dir name="Silverlight">
    <file name="Sams - Silverlight 4 Unleashed.pdf" />
    <file name="Silverlight 2 Unleashed.pdf" />
    <file name="WhatsNewInSilverlight4.pdf" />
  </dir>
  <dir name="Windows Phone">
    <file name="11180349_Building_Windows_Phone_Apps_-_A_Developers_Guide_v7_NoCover (1).pdf" />
    <file name="Programming Windows Phone 7.pdf" />
  </dir>
  <dir name="WPF">
    <file name="Building Enterprise Applications with WPF and the MVVM Pattern (pdf).pdf" />
    <file name="Prism4.pdf" />
    <file name="WPF Binding CheatSheet.pdf" />
  </dir>
</dir>

回答by the_lotus

It's a bit hard to know what's the problem you are having.

很难知道您遇到了什么问题。

You'll need to use DirectoryInfo.GetFilesand DirectoryInfo.GetDirectoriesto get the list of files and folder, loop with recursion. Then use the Xml.XmlDocument to write the xml document.

您需要使用DirectoryInfo.GetFilesDirectoryInfo.GetDirectories来获取文件和文件夹列表,递归循环。然后使用 Xml.XmlDocument 编写xml 文档