簡體   English   中英

無法使用LINQ to XML獲取元素列表

[英]Cannot get list of elements using 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元素內的所有文件列表。 它僅顯示第一個<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元素中的所有File,SizeMB和BackupLocation?

將帖子

使用SelectMany()

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

SelectMany()將多個枚舉串聯在一起。

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

OUTPUT

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

您已經有一系列文件位置,例如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);
}

進行以下操作可能會更有效:

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

Descendants()將返回文檔中所有節點的集合。 在上面的示例中,這沒什么大不了的,但是如果您構建一個適當的大文檔,這可能會減慢速度。 相反,如果您知道只想使用Files,那么使用Element()直接導航到它會更快(並且更具可讀性)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM