Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to XML Selecting Child Elements

Tags:

c#

linq-to-xml

I am trying to extract information from an XML file into an object using LINQ to XML. Although I can return the document and section Id attributes I cannot get access to the Items for each section element, it returns an IEnumerable of all the items in the document. I know this is correct as I’m calling Descendants but am struggling to get it to return only the child items of each section element. Can anybody help?

XML Document

<root>
<document id="1">
  <section id="1.1">
    <item id="1.1.1"></item>
    <item id="1.1.2"></item>
    <item id="1.1.3"></item>
  </section>
  <section id="1.2">
    <item id="1.2.1"></item>
    <item id="1.2.2"></item>
  </section>
</document>
</root>

LINQ Query

XElement documentRoot = XElement.Load("document.xml");
var documents = (from docs in documentRoot.Descendants("document")
                 select new
                    {
                        Id = (string) docs.Attribute("id"),
                        Sections = docs.Elements("section"),
                        Items = docs.Elements("section").Elements("item")
                    }).ToList();

foreach(var doc in documents)
{
    foreach(var section in doc.Sections)
    {
        Console.WriteLine("SectionId: " + section.Attribute("Id"));  
        foreach(var item in doc.Items)
        {
            Console.WriteLine("ItemId: " + section.Attribute("Id"));
        }
    }
}
like image 442
Cragly Avatar asked Jan 18 '10 22:01

Cragly


2 Answers

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"
like image 189
Rubens Farias Avatar answered Nov 06 '22 03:11

Rubens Farias


Do you want a flat structure ?!?! (from LinqPad)

XElement documentRoot  = XElement.Parse (
@"<root> 
<document id='1'> 
  <section id='1.1'> 
    <item id='1.1.1'></item> 
    <item id='1.1.2'></item> 
    <item id='1.1.3'></item> 
  </section> 
  <section id='1.2'> 
    <item id='1.2.1'></item> 
    <item id='1.2.2'></item> 
  </section> 
</document> 
</root> 
");




var documents = (from docs in documentRoot.Descendants("section") 
                 select new 
                    { 
                        SectionId = (string) docs.Attribute("id"), 
                        Items = docs.Elements("item") 
                    }); 
 documents.Dump();

This gives 2 SectionId items that containes XElements with related items.

like image 2
salgo60 Avatar answered Nov 06 '22 05:11

salgo60