C#, XML: read all descendants of a single element



I have a following XML file:



<data>
<set1>
<entry>Entry #1</entry>
<entry>Entry #2</entry>
</set1>
</data>


and I'm trying to read all the descendants of the SET element. Im not sure If I'm confusing terms like element and descendant, so Im just gonna stop using them for now :)


Here is the C# code that I use:



List<String> list = new List<String>();
XDocument xml = XDocument.Load("file.xml");

var desc = xml.Descendants("set1");
foreach (var entry in desc) {
list.add(entry.Value);
Console.Write("element: " + entry.Value);
}


But instead of two lines of console output "Entry #1" and "Entry #2" I only get one "Entry #1Entry #2". Thanks for your help!


No comments:

Post a Comment