Show XML Items From Internet into Treeview

Hello:

Well I need some help, i have the following xml file in a domain:

< xml version="1.0" encoding="utf-8" >

<ListaDisco title="Directorio">

     <Category title="Bar">

          <disco id="B1">

               <name>Bora Bora</name>

          </disco>

          <disco id="B2">

               <name>Barzelona</name>

          </disco>

     </Category>

     <Category title="Disco">

          <disco id="D1">

               <name>Baby</name>

         </disco>

     </Category>

</ListaDisco>

Then in my windows form i have the following code:

XmlDocument Items = new XmlDocument();

Items.Load(external); // URL

TreeNode t1,t2;

foreach(XmlNode node in Items.SelectNodes("//ListaDisco"))

{

     t1 = new TreeNode(node.Attributes["title"].Value);

     treeViewDisco.Nodes.Add(t1);

     foreach(XmlNode nod in node.SelectNodes("//Category"))

     {

          t2 = new TreeNode(nod.Attributes["title"].Value);

          t1.Nodes.Add(t2);

               foreach(XmlNode n in nod.SelectNodes("//name"))

               {

                    t2.Nodes.Add(new TreeNode(n.FirstChild.Value));

               }

     }

}

now the problem the Treview have the Root Directorio and two childs Bar and Disco but, each child have the same childs, I mean

Directorio

     Bar

          Item1

          Item2

          Item3

 

     Disco

          Item1

          Item2

          Item3

When must be Bar with 2 items and Disco with just one like seem in the XML File

What I am doing wrong please help :D




Answer this question

Show XML Items From Internet into Treeview

  • cb3431

     

    foreach(XmlNode n in t2.SelectNodes("name"))


  • Tom Bowen

     Chris Lovett wrote:
    Take off the "//" in your queries.  //foo means search the entire sub-tree hierarchy for nodes named "foo".

    Thanks, but if i take off the "//" for the node "name" then the Treeview doesn't show the elements:

    foreach(XmlNode n in nod.SelectNodes("name")) // or "/name"

    {

    t2.Nodes.Add(new TreeNode(n.FirstChild.Value));

    }

    in the treeview just have the main Directorio and the childs Bar and Disco

    another suggestion



  • tangentier

    Take off the "//" in your queries.  //foo means search the entire sub-tree hierarchy for nodes named "foo".
  • Show XML Items From Internet into Treeview