Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 560229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:19:16+00:00 2026-05-13T12:19:16+00:00

I am trying to extract information from an XML file into an object using

  • 0

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"));
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:19:16+00:00Added an answer on May 13, 2026 at 12:19 pm

    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"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to extract some information from an xml file using xslt. I've used
I am trying to extract information from a tags using a regex, then return
I have an xml from which I am trying to extract some information through
I have a file which I'm trying to extract information from, the file has
im trying to extract some files from a jar-file downloaded using java-webstart. below code
I'm trying to extract some information from a webpage using php cURL+preg_match or any
I am trying to extract some information from a binary file. It looks like
I am trying to extract graph information from a text file and converting it
I'm trying to extract EXIF information from images using jQuery. I've tried using a
I’m working with java Scanner trying to extract product information from a text file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.