populate a Treeview from XML file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JustRun
    New Member
    • Mar 2008
    • 127

    populate a Treeview from XML file

    Hi,
    I have this XML file , converted from a database view
    Code:
    <Parent>
      <Parent name="Category">
        <Child name="SubCat01" level="1">
          <Child name="SubCat02" level="2">
            <Child name ="Product01-01" level="3"/>
            <Child name ="Product01-02" level="3"/>
            <Child name ="Product01-03" level="3"/>
          </Child>
        </Child>
        <Child name="SubCat02" level="1">
          <Child name="Product02-01" level="2"/>
          <Child name="Product02-02" level="2"/>
        </Child>
      </Parent>
    </Parent>
    Regarding that I have an unlimited Childs.

    Code:
    TreeNode stateNode;
    TreeNode regionNode;
    
    XElement doc = XElement.Load(txtXMLPath.Text);
    
    int level = 1;
    foreach (XElement parent in doc.Descendants("Parent"))
    { 
        stateNode = treeProduct.Nodes.Add(parent.Attribute("name").Value);
        for (int i = 0; i <= level; i++)
        {
             XName childNod = "Child" + level.ToString();
             foreach (XElement child in parent.Descendants("Child"))
             {
                  regionNode = stateNode.Nodes.Add(child.Attribute("name").Value);
              }
    
              stateNode = regionNode;
              //level++;
          }
    }
    Thanks
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by OriginalPoster
    I have xxx, then do yyy then zzz happens. Here's my code ...
    Ok. You know what you have.
    What you don't have is a question. Nobody here knows why you posted this since you haven't asked anything. What is it you are looking for?
    I recommend you read the FAQ about How to ask a good question so the volunteers will be able to help you.

    Comment

    • JustRun
      New Member
      • Mar 2008
      • 127

      #3
      yes, you are right, Sorry for that.

      Actually, I have this database view:

      =============== =============== =============== =========|
      TableId | ParentId | ChildId | ParentName | ChildName |
      newId() | uniqueIdentifie r | uniqueIdentifie r | nvarchar | nvarchar |
      =============== =============== =============== =========|

      what I need is to display this view in a treeview like this:

      Parent
      Child1
      child1-1
      child1-2
      Child2
      child2-1
      child2-2

      and so on .. Regarding that the user can insert unlimited children (N categogry)

      I thought to convert this to an XML file then read it in the treeview, but none of my ways give me the accurate result

      So, I need to read my stored data in a treeview,.
      Please provide me with the best way to do this whether XML or directly from the database or dataset ... whatever.

      Thanks

      Comment

      • JustRun
        New Member
        • Mar 2008
        • 127

        #4
        I think I got itttttttttttt

        I'll put it here - Just finish then give it to u Bytes :D

        Comment

        Working...