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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:35:19+00:00 2026-05-15T10:35:19+00:00

This is the test xml that i am using: <categories> <category id=1 name=Test1> <category

  • 0

This is the test xml that i am using:

<categories>
<category id="1" name="Test1">
    <category id="2" name="Test2">
        <misc id="1"></misc>
    </category>
</category>    
<category id="3" name="Test3">
    <misc id="2"></misc>
</category>    

Now i want to bind that to an ASPX treeview, i want only the elements that have the name category and i want the name of those to appear in the treeview.

Its easy to get the id and names:

var d = from t in data.Descendants("category") 
   select new { ID = t.Attribute("id").Value, Name = t.Attribute("name").Value };

but how do i keep the structure in the treeview?

This should look like this:

Test1

-> Test2

Test3

  • 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-15T10:35:20+00:00Added an answer on May 15, 2026 at 10:35 am

    Maybe something like this if I understand you correctly? (I have not tested it)

    class Category
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public IEnumerable<Category> SubCategories { get; set; }
    }
    
    IEnumerable<Category> CategoryTreeStructure(XElement e)
    {
       var d = from t in e.Elements("category")
               select new Category()
               { 
                   ID = t.Attribute("id").Value, 
                   Name = t.Attribute("name").Value,
                   SubCategories = CategoryTreeStructure(t)
               };
    
       return d;
    }
    

    Call it with:

    var structure = CategoryTreeStructure(doc.Root);
    

    “i want only the elements that have the name category” – I do not understand what you mean here? But if you only want to select those elements which have a “name” attribute then this should work:

       ...
       var d = from t in e.Elements("category")
               where t.Attribute("name") != null
               select new Category()
               ...
    

    I understand that the upper (the “name” attribute part) is not what you wanted but I leave it there. I have tested the code against:

    XDocument doc = XDocument.Parse(@"<categories>
    <category id=""1"" name=""Test1"">
        <category id=""2"" name=""Test2"">
            <misc id=""1""></misc>
        </category>
    </category>    
    <category id=""3"" name=""Test3"">
        <misc id=""2""></misc>
    </category>  
    </categories>");
                var structure = CategoryTreeStructure(doc.Root);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML string that is loaded into an XMLDocument, similar to the
I have an external xml file that I am loading in my views.py file
I'm using JSF2 and Richfaces 4. I've added the following to my web.xml: <mime-mapping>
I have an assembly which contains several UserControl objects that I want to be
another fun (and probably really simple) question for you, that I have half worked
I created a WCF Service that I'm passing a stream to on a request.
For some reason OpenCover is not covering tests using moles on Windows Server 2003
I'm writing a UCMA 2.0 application which connects with a 'UserEndpoint' to OCS. I
I'm trying to create a custom point cache format for Houdini and 3D Max.
I have implemented a programm with the strategy pattern. So I have an interface

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.