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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:16:04+00:00 2026-06-04T02:16:04+00:00

I have an XML structure like this: <?xml version=1.0 encoding=utf-8?> <categories version=2.0> <category> <name>cat1</name>

  • 0

I have an XML structure like this:

<?xml version="1.0" encoding="utf-8"?>
<categories version="2.0">
  <category>
    <name>cat1</name>
    <category>
      <name>cat 1.1</name>
    </category>
    <category>
      <name>cat 1.2</name>
    </category>
  </category>
</categories>

I try to use the following code to covert this XML to objects:

XElement root = XDocument.Load(categoryXmlFile).Descendants("categories").First();
CategoryXml cat = new CategoryXml(root);

class CategoryXml
{
    public string Name { get; set; }
    public int Level { get; set; }
    public int Order { get; set; }
    public CategoryXml Parent { get; set; }
    public List<CategoryXml> Children { get; set; }

    CategoryXml() { }

    public CategoryXml(XElement root)
    {
        Name = "Root Category";
        Level = 0;
        Order = 1;
        Parent = null;
        Children = GetSubCategories(root, Level, this);
    }


    private List<CategoryXml> GetSubCategories(XElement parentElement, int level, CategoryXml parentCategory)
    {
        int order = 1;
        level++;

        var s = from childElement in parentElement.Elements("category")
                select new CategoryXml
                {
                    Name = childElement.Element("name").Value,
                    Level = level,
                    Order = order++,
                    Parent = parentCategory,
                    Children = GetSubCategories(childElement, level, this)
                };

        return s.ToList();
    }
} 

However, the Parent property for every sub category says “Root Category.” Instead, the Parent property for “cat 1.1” should say “cat1.”

What am I missing here?

  • 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-06-04T02:16:06+00:00Added an answer on June 4, 2026 at 2:16 am

    In GetSubCategories, you’re making the parent of every child the current object’s parent:

    select new CategoryXml
    {
        ...
        Parent = parentCategory,
        ...
    }
    

    I think you meant:

    select new CategoryXml
    {
        ...
        Parent = this,
        ...
    }
    

    After all, the parent of each child is the object creating the child, right?

    I would suggest, however, that you actually call the constructor within your select clause, like this:

    private CategoryXml(XElement current, string name, int level, int order,
                        CategoryXml parent)
    {
        Name = name;
        Level = level;
        Order = order;
        Parent = parent;
        Children = current.Elements("category")
                          .Select((child, index) => new CategoryXml(child,
                                     (string) child.Element("name"),
                                     Level + 1,
                                     index + 1,
                                     this))
                          .ToList();
    }
    
    public CategoryXml(XElement root)
    {
        this(root, "Root Category", level: 0, order: 1, parent: null);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document with a basic structure like this: <?xml version=1.0 encoding=UTF-8
I have an XML Sitemap like this <?xml version=1.0 encoding=utf-8 ?> <Menu> <MenuItem Name=Page
I have an XML file that looks like this: <?xml version=1.0? encoding=UTF-8 standalone=no?> <dir
I have an XML file with this structure: <?xml version=1.0 encoding=utf-8 ?> <Photobank> <Landowner
I have a plist structured like this: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD
I have this xml: <?xml version=1.0 encoding=utf-8?> <xml> <head> <name>This is my XML</name> <version>This
I have xml structure like this: <Group id=2 name=Third parentid=0 /> <Group id=6 name=Five
I have an XML structure that is 4 deep: <?xml version=1.0 encoding=utf-8?> <EmailRuleList xmlns:xsd=EmailRules.xsd>
I have the following simplified XML structure: <?xml version="1.0" encoding="UTF-8" ?> <INVOIC02> <IDOC BEGIN="1">
I have the following XML structure: <?xml version=1.0 encoding=utf-8 standalone=yes?> <Root xmlns:xsi=My Program> <NotRoot

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.