Cannot get list of elements using Linq to XML

Posted by Blackator on Stack Overflow See other posts from Stack Overflow or by Blackator
Published on 2012-09-05T01:53:53Z Indexed on 2012/09/05 3:38 UTC
Read the original article Hit count: 97

Filed under:
|

Sample 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>

I've been doing some tutorials but I am unable to get all the list of file inside the files element. It only shows the first element and doesn't display the rest. This is my code:

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

How do I display all the File in the Files element, the SizeMB and BackupLocation? Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about linq-to-xml