title: XML Node Listing in Tree View
body: <p>Why is my code return an Error</p>
  
  <p><code>Object reference not set to an instance of an object.</code>
   in 
  <code>treeNode.Nodes[el.Name].Nodes.Add(nodes);?</code></p>
  
  <p>i load xml file to be the menu in my treeview so that when i click it. it will load the url to my webbrowser..
  i want to output in treeview for example:</p>
  
  <pre><code>-Home
      -About
           -MoreAbouts
           -MoreAbouts
      -About
  -Index
      -About
           -MoreAbouts
           -MoreAbouts
      -About
  </code></pre>
  
  <p>i use this to achieve. but it returns an Error</p>
  
  <p><strong>my XML file is</strong></p>
  
  <pre><code>&lt;ListOfTopics&gt;
    &lt;Introduction url="index.html" title="Introduction"&gt;
          &lt;Iframe url="iframe.html" title="Iframe"&gt;
              &lt;Iframe2 url="iframe.html" title="Iframe2"/&gt;
              &lt;/Iframe&gt;
      &lt;/Introduction&gt;
    &lt;Topic url="about.html" title="About"/&gt;
  &lt;/ListOfTopics&gt;
  </code></pre>
  
  <p><strong>C# Code</strong>   </p>
  
  <pre><code>private void XmlElementHasChild(bool index,XmlElement el, TreeView treeNode)
      {
          if (index)
          {
              if (el.HasChildNodes)
              {
                  foreach (XmlElement es in el.ChildNodes)
                  {
                      TreeNode nodes = new TreeNode();
                      nodes.ToolTipText = es.GetAttribute("url");
                      nodes.Name = es.GetAttribute("title");
                      nodes.Text = es.GetAttribute("title");
                      treeNode.Nodes.Add(nodes);
                      XmlElementHasChild(true,es,treeNode);
                  }
              }
          }
          else
          {
              if (el.HasChildNodes)
              {
  
                  foreach (XmlElement es in el.ChildNodes)
                  {
                      TreeNode nodes = new TreeNode();
                      nodes.ToolTipText = es.GetAttribute("url");
                      nodes.Name = es.GetAttribute("title");
                      nodes.Text = es.GetAttribute("title");
                      treeNode.Nodes[el.Name].Nodes.Add(nodes);
                      XmlElementHasChild(false, es, treeNode);
                 } 
              }
          }
      }
      private void TreeViewLoadXml(bool index, TreeView treeNode)
      {
          XmlDocument xml = new XmlDocument();
          xml.Load(topics);
          int i = 0;
          foreach (XmlElement el in xml.DocumentElement.ChildNodes)
          {
              TreeNode node = new TreeNode();
              node.ToolTipText = el.GetAttribute("url");
              node.Name = el.GetAttribute("title");
              node.Text = el.GetAttribute("title");
  
              treeNode.Nodes.Add(node);
  
              if (index)
              {
                  XmlElementHasChild(true, el, treeNode);
              }
              else
              {
                  XmlElementHasChild(false,el, treeNode);
              }
              i++;
          }
  
          if (index)
          {
              treeNode.Sort();
          }
      }
  </code></pre>
  
  <p>This is The fix.Sorry</p>
  
  <pre><code>private void XmlElementHasChild(bool index,XmlElement el, TreeView treeNode)
          {
                  if (el.HasChildNodes)
                  {
                      foreach (XmlElement es in el.ChildNodes)
                      {
                          TreeNode nodes = new TreeNode();
                          nodes.ToolTipText = es.GetAttribute("url");
                          nodes.Name = es.GetAttribute("title");
                          nodes.Text = es.GetAttribute("title");
                          treeNode.Nodes.Add(nodes);
                          XmlElementHasChild(true,es,treeNode);
                      }
                  }
          }
  </code></pre>