Results 1 to 11 of 11

Thread: [Resolved][3.0/LINQ] XML and LINQ, Node/Element list problem

Threaded View

  1. #1

    Thread Starter
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Resolved [Resolved][3.0/LINQ] XML and LINQ, Node/Element list problem

    Hi,

    I have an XML file which populates an ASP.Net Treeview. It's basically built up like this:

    Code:
    <root>
       <Category Name="Something">
          <Item Someattribute="Something" />
          <Item Someattribute="Something" />
       </Category>
       <Category Name="Something">
          <Category Name="Something">
             <Item Someattribute="Something" />
             <Item Someattribute="Something" />
          </Category>
          <Item Someattribute="Something" />
          <Item Someattribute="Something" />
       </Category>
    </root>
    Now, this works perfectly, every subelement, get's added just like in the XML file. (Yes, I am confusing elements with nodes, or vice versa - if someone could give me a clear explanation on what the difference is, that'd be great ).

    What I now want to is load this XML file on my own, that I might create a fancy admin for the treeview. The idea I came up with was to create a recursive method that would call itself each time it hit a Category node(?). So, I made this:

    Code:
        private static void fillCategories(IEnumerable<XElement> list, List<XMLToolboxCategory> CatList)
        {
            var q = from x in list
                    select x;
    
            foreach (var x in q)
            {
                var CatItems = new List<XMLToolboxItem>();
                var Cat = new XMLToolboxCategory();
    
                var qItems = from itm in x.Elements()
                             select itm;
    
                foreach (var itm in qItems)
                {
                    if (itm.Name == "Category")
                    {
                        Cat.CategoryList = new List<XMLToolboxCategory>();
                        fillCategories(itm.Descendants("Category"), Cat.CategoryList);
                    }
                    else
                    {
                        CatItems.Add(new XMLToolboxItem() { Filename = itm.Attribute("Filename").Value, Value = itm.Attribute("Value").Value, Type = itm.Name.ToString(), parent = itm.Parent });
                    }
                }
                CatList.Add(new XMLToolboxCategory() { Name = x.Attribute("Name").Value, ToolList = CatItems });
            }
        }
    The two classes XMLToolboxItem and XMLToolboxCategory basically just hold some objects, it's rather straight forward. One important thing though, is that each XMLToolboxCategory can hold another Category and so forth, to implement the hierarchy from the XML into my objects.

    I then call my method simply with:

    Code:
                    Categories = new List<XMLToolboxCategory>();
                    var xtbox = XDocument.Load(filePath);
    
                    fillCategories(xtbox.Descendants("Category"), Categories);
    The result is, that i load every element(?) just fine. However, my hierarchy isn't preserved - the method fillCategories() never calls itself.

    So, I figure the problem lies in the list that XElement/XDocument.Descendants(XName) returns - it must return a complete list of all the elements(?) within all the Category nodes(?) instead of just the ones relative to it.

    I hope I'm not sounding to cryptic on this one - as always, any help is appreciated.

    Thanks in advance.
    Last edited by vbNeo; Mar 12th, 2008 at 03:40 PM.
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width