无法使用LINQ to XML获取元素列表

时间:2012-09-05 01:53:53

标签: c# linq-to-xml

示例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元素中的所有文件列表。它只显示第一个&lt; File&gt;元素并不显示其余部分。这是我的代码:

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 个答案:

答案 0 :(得分:2)

[被修改]

使用SelectMany()

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

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

答案 1 :(得分: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

答案 2 :(得分:2)

您已经拥有一系列文件位置,例如Aaron Anodide建议您这样做。在forforeach循环中使用此数组:

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);
}

答案 3 :(得分:1)

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

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

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