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 7459121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:15:51+00:00 2026-05-29T14:15:51+00:00

I want to build a C# object from hierarchical XML data usinq LINQ. I

  • 0

I want to build a C# object from hierarchical XML data usinq LINQ.
I have loaded the XML as an XDocument (by reading the XML from a file into a string first).
I need some guidance on how I should parse this.

Example string read from XML file as

<?xml version="1.0" encoding="utf-8" ?>
<categories version="1.0">
  <category id="0" name="women" description="test">
    <category id="01" name="tops" description="test"></category>
    <category id="02" name="bottoms" description="test"></category>
    <category id="03" name="accessories" description="test"></category>
  </category>
  <category id="1" name="men" description="test">
    <category id="11" name="shirts" description="test"></category>
    <category id="12" name="trousers" description="test"></category>
    <category id="13" name="accessories" description="test"></category>
  </category>
  <category id="2" name="kids &amp; baby" description="test" />
  <category id="3" name="home &amp; living" description="test" />
</categories>

And I have such a POCO class:

[DataContract]
public class Category
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Description { get; set; }

    [DataMember]
    public List<Category> SubCategories { get; set; }
}
  • 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-29T14:15:53+00:00Added an answer on May 29, 2026 at 2:15 pm

    You have two options.

    1. Use .NET serialization, in which case you need to specify the XML mappings by decorating your POCO class with appropriate attributes (property name ⇄ XML element name).

    2. Use LINQ to XML (like you want do). In that case, the code could look something like this:

      var categories = x.Root.Elements().Select(e =>
          new Category
          {
              Id = int.Parse(e.Attribute("id").Value),
              Name = e.Attribute("name").Value,
              Description = e.Attribute("description").Value,
              SubCategories = e.Elements().Select(e1 =>
                  new Category
                  {
                      Id = int.Parse(e1.Attribute("id").Value),
                      Name = e1.Attribute("name").Value,
                      Description = e1.Attribute("description").Value
                  }).ToList()
          }).ToList();
      

      Or recursively, by adding a recursive method Parse to your class:

      public static Category Parse(XElement value)
      {
          return new Category
          {
              Id = int.Parse(value.Attribute("id").Value),
              Name = value.Attribute("name").Value,
              Description = value.Attribute("description").Value,
              SubCategories = value.Elements().Select(newvalue =>  Parse(newvalue)).ToList()
          };
      }
      

      and calling it like this:

      var categories = x.Root.Elements().Select(e => Category.Parse(e)).ToList();
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build an XML document from a Java object hierarchy. Both the
In my build action, I have an object that I put into my session
I am trying to extract values from an XML file using linq to create
I want to render an object imported from wavefront obj file. The object has
I want to build nested json object using python jsonpickle, something like this {key:
I want to build a 2 dimensional (non ragged at this point) object array.
I want to build a form where users can enter some data in a
I have a schema (xsd), and I want to create xml files that conform
I have a text file in my computer which I am reading form my
I want to create a functional object which could be called later (callback) build

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.