创建xdocument结构
我正在尝试将驱动器索引到 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;
}
这里有一些输出 - 简单的文件结构 - root 有 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>
I am trying to index a drive to a xml file. My pitiful attempt is this:
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;
}
some output here - simple file structure - root has 2 mp3s and 1 folder containing the 2 files.
<?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>
I am getting some strangely intermixed tags there and I simply don't get it. Any help appreciated.
using od's loop structure it changes to that:
<?xml version="1.0" encoding="utf-8"?>
<root>directory<root>directory</root>
<file>E:\180.mp3</file>
<file>E:\181.mp3</file>
</root>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下循环结构:
这样您将在目录中的目录之后立即显示属于该目录的文件。
Try the following looping structure:
This way you will display the files belonging to a directory immediatly after the directories in it.