【发布时间】:2010-01-21 13:48:24
【问题描述】:
我正在尝试将驱动器索引到 xml 文件。我可怜的尝试是这样的:
internal static void createIndex(String path, String driveLabel)
{
XDocument w = new XDocument();
w.Add(createStructure(path, new XElement("root")));
w.Save(driveLabel +".xml");
}
internal static XElement createStructure(String path, XElement x)
{
try
{
String[] files = Directory.GetFiles(path);
String[] folders = Directory.GetDirectories(path);
foreach (String s in folders)
{
x.Add("directory", createStructure(s, x));
}
foreach (String f in files)
{
x.Add(new XElement("file", f));
}
}
catch (Exception e) { }
return x;
}
这里有一些输出 - 简单的文件结构 - 根目录有 2 个 mp3 和 1 个包含 2 个文件的文件夹。
<?xml version="1.0" encoding="utf-8"?>
<root>
<file>E:\.wd_tv\ph.db</file>
<file>E:\.wd_tv\ph.db-journal</file>directory<root>
<file>E:\.wd_tv\ph.db</file>
<file>E:\.wd_tv\ph.db-journal</file>directory</root>
<file>E:\180.mp3</file>
<file>E:\181.mp3</file>
</root>
我在那里得到了一些奇怪的混合标签,但我根本不明白。任何帮助表示赞赏。
使用 od 的循环结构会变成这样:
<?xml version="1.0" encoding="utf-8"?>
<root>directory<root>directory</root>
<file>E:\180.mp3</file>
<file>E:\181.mp3</file>
</root>
【问题讨论】:
-
你得到了hoodywha?抛出一些错误的输出,因为代码看起来是正确的。
标签: c# .net xml linq-to-xml