You got some small typos, on attribute Id
and at item loop. But if you're trying to get all section items into that items collection, you're wrong at design level, as Items
should be a Section
property, not Document
(so you'll need to query your XML twice).
Or, you can to do something like:
var documents =
(from docs in documentRoot.Descendants("document")
select new
{
Id = (string) docs.Attribute("id"),
Sections = docs.Elements("section")
}).ToList();
foreach (var doc in documents)
{
foreach (var section in doc.Sections)
{
Console.WriteLine("SectionId: " + section.Attribute("id"));
foreach (var item in section.Elements("item"))
{
Console.WriteLine("ItemId: " + item.Attribute("id"));
}
}
}
Output:
SectionId: id="1.1" ItemId: id="1.1.1" ItemId: id="1.1.2" ItemId: id="1.1.3" SectionId: id="1.2" ItemId: id="1.2.1" ItemId: id="1.2.2"
Rubens Farias
2010-01-18 23:07:22