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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:23:44+00:00 2026-05-27T04:23:44+00:00

I have this XML which has to be Parsed <category id=1> <title>Environment & Heritage</title>

  • 0

I have this XML which has to be Parsed

<category id="1">
    <title>Environment & Heritage</title>
    <subcategories>
        <subcategory id="2">Trees & Green Cover</subcategory>
        <subcategory id="3">Noise Pollution</subcategory>
        <subcategory id="4">Air Pollution</subcategory>
        <subcategory id="5">Water Pollution</subcategory>
</category>
<category id="72">
    <title>Environment and Heritage</title>
    <subcategories/>
</category>
<category id="7">
    <title>Health & Sanitation</title>
    <subcategories><subcategory id="8">Drains & Sewerage</subcategory>
    <subcategory id="9">Solid Waste Management</subcategory>
    </subcategories>
</category>

The class is

public class Categories
{
    public string Id { get; set; }
    public string Title { get; set; }
    public List<Categories> subCategories { get; set; }

    public Categories() { }

    public Categories(string value, string text)
    {
        this.Id = value;
        this.Title = text;
    }
}

we have list of object of this class List

this function is doing the main work

List<Categories> FillObjectFromXML(string xmString)
    {
        //Declaration
        XDocument xmlDoc = XDocument.Load(new StringReader(xmString));
        List<Categories> categoriesList = new List<Categories>();
        Categories catItem;// = new Categories();
        Categories subItem;
        List<Categories> subCategoriesList;// = new List<Categories>();

        //Coding
        var lv1s = from lv1 in xmlDoc.Descendants("category")
                   select new
                   {
                       Id = lv1.Attribute("id").Value,
                       Header = lv1.Descendants("title"),
                       Children = lv1.Descendants("subcategories")
                   };

        //Loop through results
        foreach (var lv1 in lv1s)
        {
            catItem = new Categories();
            catItem.Id = lv1.Id;
            catItem.Title = lv1.Header.First().Value;
            subCategoriesList = new List<Categories>();
            foreach (var lv2 in lv1.Children) 
            {
                subItem=new Categories();
                subItem.Id=lv2.Attribute("id").Value;
                subItem.Title=lv2.Descendants("title").ToString();
                subCategoriesList.Add(subItem);
            }
            catItem.subCategories = subCategoriesList;
            categoriesList.Add(catItem);
        }


        //End
        return categoriesList;
    }

foreach loop for lv2 is not getting the right results

  • 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-27T04:23:45+00:00Added an answer on May 27, 2026 at 4:23 am

    In you method above I believe you need to change the line:

    Children = lv1.Descendants("subcategories")
    

    to:

    Children = lv1.Descendants("subcategory")
    

    If you like XPath you could probably simplify this code.

    Level one categories:

    XmlNodeList categories = xmlDoc.SelectNodes("//category"); //assumes there is only one tier of categories.
    

    You can then foreach round each of the categories:

    foreach(XmlNode category in categories)
    {
      XmlNodeList subcategories = category.SelectNodes("./subcategories/subcategory");
    }
    

    Thus removing much of the DOM walking you have to do. Depends on if you like XPath or not – but it’s great for this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this xml file which has text init. i.e Hi my name is
I have a string which has an xml inside. I want to write this
I have XML data as a string which has to parsed, I am converting
I have this ugly XML which has alot of namespaces on it, when I
I have a xml which has several numbers, something like this: <xml> <case1> <amount>3.140000</amount>
I have this xml from which I am trying to grab the value in
I have this XML of products which I'm trying to validate. (i keep it
I currently have this code which stores XML into an XML-type column called data,
I have some XML which contains records and sub records, like this: <data> <record
I have an XML document which looks something like this: <meadinkent> <record> <comp_div>MENSWEAR</comp_div> <sty_ret_type>ACCESSORIES</sty_ret_type>

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.