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

  • Home
  • SEARCH
  • 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 1114905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:04:35+00:00 2026-05-17T03:04:35+00:00

I am new to LINQ to XML and need help in mapping an XML

  • 0

I am new to LINQ to XML and need help in mapping an XML hierarchy into my domain object. Here is the XML source:

<?xml version="1.0" encoding="UTF-8"?>
<Listings>
    <Region>United States</Region>
    <Listing>
        <CatID>ELE</CatID>
        <CatDesc>Electronics</CatDesc>
        <ItemID>ELE_LCDTV</ItemID>
        <ItemDesc>LCD TV BLU RAY</ItemDesc>
        <TotalPrice>1500</TotalPrice>
    </Listing>
    <Listing>
        <CatID>COMP</CatID>
        <CatDesc>Computer</CatDesc>
        <ItemID>COMP_LAPTOP</ItemID>
        <ItemDesc>Laptop HP</ItemDesc>
        <TotalPrice>1200</TotalPrice>
    </Listing>
    <Listing>
        <CatID>MISC</CatID>
        <CatDesc>Miscellaneous</CatDesc>
        <ItemID>MISC_WII</ItemID>
        <ItemDesc>Wii</ItemDesc>
        <TotalPrice>350</TotalPrice>
    </Listing>
    <Listing>
        <CatID>COMP</CatID>
        <CatDesc>Computer</CatDesc>
        <ItemID>COMP_HD</ItemID>
        <ItemDesc>Hard Disk</ItemDesc>
        <TotalPrice>300</TotalPrice>
    </Listing>
    <Listing>
        <CatID>ELE</CatID>
        <CatDesc>Electronics</CatDesc>
        <ItemID>ELE_IPOD</ItemID>
        <ItemDesc>iPod</ItemDesc>
        <TotalPrice>225</TotalPrice>
    </Listing>
    <Listing>
        <CatID>COMP</CatID>
        <CatDesc>Computer</CatDesc>
        <ItemID>COMP_WKEY</ItemID>
        <ItemDesc>Wireless Keyboard</ItemDesc>
        <TotalPrice>110</TotalPrice>
    </Listing>
    <Listing>
        <CatID>MISC</CatID>
        <CatDesc>Miscellaneous</CatDesc>
        <ItemID>MISC_GAME</ItemID>
        <ItemDesc>Games</ItemDesc>
        <TotalPrice>50</TotalPrice>
    </Listing>
</Listings>

I have to populate following domain objects ushing above XML. Basically I have to expose an IEnumerable ListCategories()

public class Category
{
    public string ID { get; set; }
    public string Description { get; set; }
    public IList<Item> Items { get; set; } 
}

public class Item
{
    public string ID { get; set; }
    public string Description { get; set; }
    public decimal TotalPrice { get; set; }

}

I undersyand that I have to an orderby query first to sort the XML by CatID and then traverse through it to populate my domain objects.

XDocument xDoc = XDocument.Parse(XmlizedString);

var listing = from x in xDoc.Elements("Listings").Elements("Listing")
                                           orderby (string)x.Element("CatID")
                                           select x;

Above query will sort my XML by CatID but I am not clear how to proceed further…

I would gretly appreciate your suggestions/help in resolving this.

  • 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-17T03:04:36+00:00Added an answer on May 17, 2026 at 3:04 am

    Your Linq-to-xml query would look like this.

    var listing = 
    xDoc.Elements("Listings")
    .Elements("Listing")
    .GroupBy (x => new { 
                    ID = x.Element(XName.Get("CatID")).Value
                    , Description = x.Element(XName.Get("CatDesc")).Value
                }
    )
    .Select (x => new Category { 
                    ID = x.Key.ID
                    , Description = x.Key.Description
                    , Items = x.Select (i => new Item { 
                                        ID = i.Element("ItemID").Value
                                        , Description = i.Element("ItemDesc").Value
                                        , TotalPrice = decimal.Parse(i.Element("TotalPrice").Value) 
                                       }
                        ).ToList()
                }
            )
    .OrderBy (x => x.ID);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

still new to the world of linq, and i need some help flatening a
I have the following (as an example) XML file and XSD. <?xml version=1.0 encoding=utf-8
I've got a new instance of an object in a Linq DBML, but don't
Guess people need some experience with Linq-to-xml and knowledge of the build of a
I am new to XML and Linq to XML and I just can't find
I need to parse XML string with Linq, and I came up with the
Could someone please help me how to read the XML Document below using Linq
I'm familiar with .NET and with SQL. Now I'm looking at the new LINQ
I am completely new to LINQ in C#/.NET. I understand that I could use
I am new with Linq and I would like to sort some data that

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.