title: C#/XML Populate TreeView with XML file
body: <p>im trying to populate a treeview from a xml file.</p>
  
  <p>Image of the output: <a href="http://i.stack.imgur.com/3HSCu.png" rel="nofollow">http://i.stack.imgur.com/3HSCu.png</a></p>
  
  <p>So as you can see the parents are loaded well, but the childs (the elements) not. All the child nodes are loaded in all parent nodes. But the xml is not like that.</p>
  
  <p>This is the XML code:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; 
  &lt;toolbox&gt;
  &lt;parent id="p1" caption="All Elements" class="parent"&gt;
    &lt;element id="1" name="Button" /&gt;
    &lt;element id="2" name="Label" /&gt;
    &lt;element id="3" name="Inputfield" /&gt;
    &lt;element id="4" name="Textarea" /&gt;
    &lt;element id="5" name="Image" /&gt;
    &lt;element id="6" name="Background" /&gt;
    &lt;element id="7" name="TreeView" /&gt;
  &lt;/parent&gt;
  &lt;parent id="p2" caption="Some Elements 1" class="parent"&gt;
    &lt;element id="1" name="Button" /&gt;
    &lt;element id="2" name="Label" /&gt;
    &lt;element id="3" name="Inputfield" /&gt;
  &lt;/parent&gt;
  &lt;parent id="p3" caption="Some Elements 2" class="parent"&gt;
    &lt;element id="4" name="Textarea" /&gt;
    &lt;element id="5" name="Image" /&gt;
    &lt;element id="6" name="Background" /&gt;
    &lt;element id="7" name="TreeView" /&gt;
  &lt;/parent&gt;
  &lt;/toolbox&gt;
  </code></pre>
  
  <p>This is the C# code:</p>
  
  <pre><code>    public void loadElements(string XML_Elements, TreeView Elements_Tree){
          XmlDocument XMLDocument =  new XmlDocument();
          XMLDocument.Load(XML_Elements);
  
          Elements_Tree.Nodes.Clear();
          Elements_Tree.BeginUpdate();
  
          XmlNodeList XMLParent = XMLDocument.SelectNodes("toolbox/parent"); 
          foreach(XmlNode xmlparent in XMLParent){
              //add parents
              string Parent_Caption = xmlparent.Attributes["caption"].Value;
              TreeNode parents = Elements_Tree.Nodes.Add(Parent_Caption);
  
              //add childs
              XmlNodeList XMLChilds = XMLDocument.SelectNodes("toolbox/parent/element");
              foreach (XmlNode xmlchild in XMLChilds)
              {
                  string Child_Name = xmlchild.Attributes["name"].Value;
                  parents.Nodes.Add(Child_Name);
              }
          }            
      }
  </code></pre>