title: Creating a Treeview structure in c# with main nodes and subnodes
body: <p>I want to display a treeview structure from information I get from a parsed xml document. It has to display only the "ItemName" and depending on the "Level" of the node is where its suppose to be like level 1 is the parent and level 2 is the child and level 3 is the childs child(leaf).</p>
  
  <p>I am getting errors on when i want to add the subnodes or when i iterate through the xml nodes it only saves the last node and subnode.</p>
  
  <p>this is what i've done so far </p>
  
  <p>Please note, this must display on a windows form structureview</p>
  
  <pre><code>private void TreeNodeStrcutureManually(XmlDocument structure)
      {
  
  
          treeView1.Nodes.Clear();
  
          XmlElement root = structure.DocumentElement;
          XmlNodeList itemName = root.GetElementsByTagName("ItemName");
          XmlNodeList level = root.GetElementsByTagName("Level");
  
  
          string parent = "";
          string childNode1 = "";
          string childNode2 = "";
  
  
          {
              for (int i = 0; i &lt; itemName.Count; i++)
              {
  
  
  
                  TreeNode tNode;
  
  
                  if (level.Item(i).InnerText == "1")
                  {
  
                      parent = itemName.Item(i).InnerText;
                      tNode = treeView1.Nodes.Add(parent);
  
                  }
  
                  if (level.Item(i).InnerText == "2")
                  {
                      childNode1 = itemName.Item(i).InnerText;
                      tNode.Nodes.Add(childNode1);
                  }
  
                  if (level.Item(1).InnerText == "3")
                  {
  
                      childNode2 = itemName.Item(i).InnerText;
                       tNode.Nodes.Add(childNode2);
                  }
                }
             }
  </code></pre>
  
  <p>Here is an example of how the xml is returned:       </p>
  
  <pre><code>                  &lt;HR&gt;
                       &lt;ItemName&gt; Addidas&lt;/ItemName&gt;
                         &lt;level&gt; 1 &lt;/level&gt;
                    &lt;/HR&gt;
  
  
                    &lt;HR&gt;
                      &lt;ItemName&gt; Shoe &lt;/ItemName&gt;
                      &lt;level&gt; 2 &lt;/level&gt;
                     &lt;/HR&gt;
  
                    &lt;HR&gt;
                     &lt;ItemName&gt; Tshirt &lt;/ItemName&gt;
                      &lt;level&gt; 2 &lt;/level&gt;
                       &lt;/HR&gt;
  
                        &lt;HR&gt;
                       &lt;ItemName&gt; shoelace &lt;/ItemName&gt;
                       &lt;level&gt; 2 &lt;/level&gt; 
                       &lt;/HR&gt;
  
  
                         &lt;HR&gt;
                          &lt;ItemName&gt; Running &lt;/ItemName&gt;
                         &lt;level&gt; 3 &lt;/level&gt;
                        &lt;/HR&gt; 
  
                          &lt;HR&gt;
                           &lt;ItemName&gt; Nike &lt;/ItemName&gt;
                         &lt;level&gt; 1 &lt;/level&gt;
                            &lt;/HR&gt;
  </code></pre>