2

示例 XML:

<CONFIGURATION>
    <Files>
        <File>D:\Test\TestFolder\TestFolder1\TestFile.txt</File>
        <File>D:\Test\TestFolder\TestFolder1\TestFile01.txt</File>
        <File>D:\Test\TestFolder\TestFolder1\TestFile02.txt</File>
        <File>D:\Test\TestFolder\TestFolder1\TestFile03.txt</File>
        <File>D:\Test\TestFolder\TestFolder1\TestFile04.txt</File>
    </Files>
    <SizeMB>3</SizeMB>
    <BackupLocation>D:\Log backups\File backups</BackupLocation>
</CONFIGURATION>

我一直在做一些教程,但我无法获取 files 元素中的所有文件列表。它只显示第一个 <File> 元素,不显示其余元素。这是我的代码:

var fileFolders = from file in XDocument.Load(@"D:\Hello\backupconfig1.xml").Descendants("Files")
                         select new
                         {
                             File = file.Element("File").Value
                         };

foreach (var fileFolder in fileFolders)
{
     Console.WriteLine("File = " + fileFolder.File);
}

如何在 Files 元素、SizeMB 和 BackupLocation 中显示所有文件?

4

4 回答 4

2

[已编辑]

使用SelectMany()

IEnumerable<string> files = 
    XDocumentLoad(@"D:\Hello\backupconfig1.xml").Descendants("Files")
        .SelectMany(files => files.Descendants("File"))
        .Select(file => file.Value)

SelectMany() 将多个枚举连接为一个。

于 2012-09-05T02:19:54.267 回答
2
string xml = @"<CONFIGURATION> 
<Files> 
    <File>D:\Test\TestFolder\TestFolder1\TestFile.txt</File> 
    <File>D:\Test\TestFolder\TestFolder1\TestFile01.txt</File> 
    <File>D:\Test\TestFolder\TestFolder1\TestFile02.txt</File> 
    <File>D:\Test\TestFolder\TestFolder1\TestFile03.txt</File> 
    <File>D:\Test\TestFolder\TestFolder1\TestFile04.txt</File> 
</Files> 
<SizeMB>3</SizeMB> 
<BackupLocation>D:\Log backups\File backups</BackupLocation> 
</CONFIGURATION>";

var xdoc = XDocument.Parse(xml);
var configuration = xdoc.Element("CONFIGURATION");
string sizeMB = configuration.Element("SizeMB").Value;
string backupLocation = configuration.Element("BackupLocation").Value;
string[] files = configuration.Element("Files").Elements("File").Select(c => c.Value).ToArray();

Console.WriteLine(sizeMB);
Console.WriteLine(backupLocation);    
Console.WriteLine(string.Join("\r\n", files));

输出

3
D:\Log backups\File backups
D:\Test\TestFolder\TestFolder1\TestFile.txt
D:\Test\TestFolder\TestFolder1\TestFile01.txt
D:\Test\TestFolder\TestFolder1\TestFile02.txt
D:\Test\TestFolder\TestFolder1\TestFile03.txt
D:\Test\TestFolder\TestFolder1\TestFile04.txt
于 2012-09-05T03:08:01.077 回答
2

您已经有一系列文件位置,就像Aaron Anodide推荐的那样。for在orforeach循环中使用此数组:

string[] files = configuration.Element("Files").Elements("File").Select(c => c.Value).ToArray();
for (int index=0; index<files.Length; index++)
{
    DoSomeStuffWithFileCLocation(files[i]);
}

或者

forach(string file in files)
{
    DoSomeStuffWithFileCLocation(file);
}
于 2012-09-05T03:36:16.140 回答
1

执行以下操作可能会更高效:

XDocument doc = XDocument.Load(@"D:\Hello\backupconfig1.xml");
var fileFolders = doc.Root.Element("Files").Elements("File");

Descendants()将返回文档中所有节点的集合。在上面的示例中,这没什么大不了的,但是如果您构建一个适当大的文档,这可能会减慢速度。相反,如果您知道您只需要文件,那么直接导航到它Element()会更快(并且可以说更具可读性)。

于 2012-09-05T02:33:04.440 回答