如何生成构造给定文件夹的XML文件以递归方式表示其中的所有文件&子文件夹?在C#中创建表示文件夹结构(包括子文件夹)的XML文件
回答
这是一个很好的问题示例,可以使用递归算法轻松解决!
伪代码:
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
它可以用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;
}
而且例子用法:
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();
}
我的笔记本上的目录之一的输出:
<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>
对不起,要问大家,但我正在寻找类似的东西来生成一个XML(或其他jQuery可访问的文件类型)。输出目录及其内容的XML(或其他)文件将被解析并用于制作音乐播放器。 我是一个ASP经典人(刚刚进入.NET),很好奇 - 如果我要从上面的代码开始,我可以使用什么类型的文件? .ASPX?他们无论如何要在ASP.NET网页中做到这一点? – derekmx271 2013-09-11 09:23:27
这有点难以知道你有什么问题。
您需要使用DirectoryInfo.GetFiles和DirectoryInfo.GetDirectories来获取文件和文件夹列表,循环递归。然后使用Xml.XmlDocument编写xml document。
使用'XDocument'然后'XmlDocument'实现会容易得多 – MarcinJuraszek 2013-02-26 18:26:03
@Marcin好点。 – 2013-02-26 18:29:33
- 1. 创建文件夹/文件结构
- 2. 使用PHP创建文件夹选择列表 - 包括子文件夹?
- 3. 在指定文件夹中创建文件夹在新文件夹中创建子文件夹
- 4. 为每个文件夹创建x个文件夹的文件夹结构
- 5. 创建重复的文件夹和文件夹结构
- 6. 查看.m文件中的一个文件夹(包括子文件夹)在MATLAB
- 7. Windows:基于XML文件创建文件夹结构
- 8. 在文件夹bash中创建每个子文件夹的文件列表
- 9. 焦油/的bZIP文件,而不包括文件夹结构
- 10. 电子构建Windows文件夹结构
- 11. NullReferenceException在创建文件夹结构时
- 12. 如何读取文件夹中的所有txt文件? (包括子文件夹)
- 13. 用c文件夹结构创建zip文件#
- 14. C#的文件夹和子文件夹
- 15. 在C中创建一个文件夹的Zip文件夹#
- 16. 创建EXE安装用bat文件,文件夹,子文件夹
- 17. 在VS 2010中创建文件/文件夹结构
- 18. 在python中创建文件夹/子文件夹
- 19. 如何在子文件夹中创建多个文件夹.......!
- 20. 在代码点火器中创建文件夹/子文件夹
- 21. 使用C++读取文件夹内的所有文件(包括子文件夹中的文件)
- 22. Applescript:在多个文件夹中创建新的文件夹结构
- 23. 删除文件夹和所有子文件夹(包括文件夹)中的所有文件
- 24. 在tmp文件夹中创建临时文件夹c语言
- 25. VBScript:使用Excel中的文件夹结构创建文件
- 26. VB.Net - 如何更改文件夹包括子文件夹和文件的属性
- 27. 命令行:在包含空格的子文件夹中创建文件夹
- 28. 文件夹结构
- 29. 文件夹结构
- 30. CodeIgniter:基于文件夹和子文件夹的URL结构
你需要证明你尝试了一些东西! – 2013-02-26 18:21:41
请提供您尝试过的一些示例。最好还提供您期望的输出类型的示例。如果你不能提供一个完整的例子然后展示你可以做什么(例如,如果你不知道如何枚举一个目录然后将其抽象出来) – Guvante 2013-02-26 18:21:55