title: Recursion, parsing xml file with attributes into treeview c#
body: <p>I'm currently working on a project where an application will take on XML files and display it into a treeview in C#. I'm using Visual Studio 10 to write this code. </p>
  
  <p>I cannot limit number of times the attributes are displayed. I used a foreach loop to loop through each of the attributes it has and display it, but it's displaying the attributes once for each childnode it has under the node.  How can I modify this code to only display the attributes once?</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Xml;
  
  namespace xmlToTreeview
  {
      public partial class Form1 : Form
      {
          string samplePath = Application.StartupPath + @"\\sample.xml";
          public Form1()
          {
              InitializeComponent();
              DisplayTreeView(samplePath);
          }
  
          private void DisplayTreeView(string pathname)
          {
              try
              {
                  // SECTION 1. Create a DOM Document and load the XML data into it.
                  XmlDocument dom = new XmlDocument();
                  dom.Load(pathname);
  
  
                  // SECTION 2. Initialize the TreeView control.
                  treeView1.Nodes.Clear();
                  treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
                  TreeNode tNode = new TreeNode();
                  tNode = treeView1.Nodes[0];
  
                  // SECTION 3. Populate the TreeView with the DOM nodes.
                  AddNode(dom.DocumentElement, tNode);
  
              }
              catch (XmlException xmlEx)
              {
                  MessageBox.Show(xmlEx.Message);
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
          private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
          {
              XmlNode xNode;
              TreeNode tNode;
              XmlNodeList nodeList;
              int i;
  
              // Loop through the XML nodes until the leaf is reached.
              // Add the nodes to the TreeView during the looping process.
  
              if (inXmlNode.HasChildNodes)
              {
                  nodeList = inXmlNode.ChildNodes;
  
                  for (i = 0; i &lt;= nodeList.Count - 1; i++)
                  {
                      xNode = inXmlNode.ChildNodes[i];
                      inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
                      tNode = inTreeNode.Nodes[i];
  
                      //Check if the XmlNode has attributes
                      if (inXmlNode.Attributes.Count != 0)
                      {
                          foreach (XmlAttribute att in inXmlNode.Attributes)
                          {
                              inTreeNode.Text = inTreeNode.Text + " " + att.Name + ": " + att.Value;
                          }
                      }
                      AddNode(xNode, tNode);
                  }
              }
              else
              {
                  // Here you need to pull the data from the XmlNode based on the
                  // type of node, whether attribute values are required, and so forth.
                  inTreeNode.Text = (inXmlNode.OuterXml).Trim();
              }
              treeView1.ExpandAll();
          }
      }
  }
  </code></pre>
  
  <p>And here is an example of my xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;DataConfiguration xmln="abcefg12345" xmlns:xsi="12345abcefg" xsi:schemaLocation="12345abcefg12345abcefg"&gt;
    &lt;Hosts&gt;
      &lt;Sites&gt;
          &lt;Site Name="ss"&gt;
              &lt;Host Id="aa"&gt;
                  &lt;Address Host="www.www.com"&gt; &lt;/Address&gt;
              &lt;/Host&gt;
              &lt;Host Id="ee"&gt;
                  &lt;Address Host="www.www.com"&gt; &lt;/Address&gt;
              &lt;/Host&gt;
              &lt;Host Id="dd"&gt;
                  &lt;Address Host="www.www.com"&gt; &lt;/Address&gt;
              &lt;/Host&gt; 
              &lt;Host Id="pp"&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com/"/&gt;
              &lt;/Host&gt;
              &lt;Host Id="ss"&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
              &lt;/Host&gt; 
              &lt;Host Id="561"&gt;
                  &lt;Address Host="www.www.com"&gt; &lt;/Address&gt;
              &lt;/Host&gt; 
          &lt;/Site&gt;
          &lt;Site Name="hihi"&gt;
              &lt;Host Id="cc"&gt;
                  &lt;Address Host="www.www.com"&gt; &lt;/Address&gt;
              &lt;/Host&gt;
              &lt;Host Id="sdD"&gt;
                  &lt;Address Host="www.www.com"&gt; &lt;/Address&gt;
              &lt;/Host&gt;
              &lt;Host Id="8uj"&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
  
              &lt;/Host&gt;
              &lt;Host Id="222"&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;               
              &lt;/Host&gt;
              &lt;Host Id="hhh"&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
              &lt;/Host&gt;
              &lt;Host Id="hhh"&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
              &lt;/Host&gt;             
          &lt;/Site&gt;     
      &lt;/Sites&gt;
              &lt;Host Id="hhh"&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
              &lt;/Host&gt;
              &lt;Host Id="hhh"&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
              &lt;/Host&gt;
              &lt;Host Id="hhh"&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
              &lt;/Host&gt;         
              &lt;Host Id="hhh"&gt;
                  &lt;Address Scheme="ppp" Host="www.www.com" Path="www.www.com"/&gt;
              &lt;/Host&gt;
  
  &lt;/Hosts&gt;
  &lt;DataPools&gt;
      &lt;DataPool Id="sss" default="sure"&gt;
          &lt;DataGroup Id="sss" Parent="aaa" UserCanSelectHost="sure" &gt;
              &lt;HostId Parent="hhhh"&gt;I'm breaking here&lt;/HostId&gt;
              &lt;DataSources&gt;
                  &lt;empty/&gt;
              &lt;/DataSources&gt;
          &lt;/DataGroup&gt;
          &lt;DataGroup Id="ccc" UserCanSelectHost="whynot" &gt;
              &lt;HostId&gt;God I'm breaking here again, i hope you can fix me&lt;/HostId&gt;
              &lt;DataSources&gt;
                  &lt;empty/&gt;
              &lt;/DataSources&gt;
          &lt;/DataGroup&gt;
          &lt;DataGroup Id="sss" UserCanSelectHost="yessure" &gt;
              &lt;HostId&gt;cry face&lt;/HostId&gt;
                  &lt;webfg displaygroup="sss" provider="sss" id="ccc" principal="ccc" nioarc="ccc" nap="ccc" group="ccc"&gt;
                  &lt;/webfg&gt;
  
                  &lt;nhood port="1234"/&gt;
              &lt;ServerNames&gt;
                &lt;!-- insert comment --&gt;
                &lt;!-- insert comment --&gt;
                &lt;!-- insert comment --&gt;
                &lt;ServerName&gt;myname&lt;/ServerName&gt;
                &lt;ServerName&gt;yourname&lt;/ServerName&gt;
              &lt;/ServerNames&gt;
              &lt;!-- insert comment --&gt;
              &lt;Implementations&gt;
                &lt;Implementation&gt;
                  &lt;Name&gt;yourname&lt;/Name&gt;
                  &lt;Type&gt;typeme&lt;/Type&gt;
                  &lt;Assembly&gt;visionme&lt;/Assembly&gt;
                  &lt;Path&gt;ohno&lt;/Path&gt;
                &lt;/Implementation&gt;
              &lt;/Implementations&gt;--&gt;
                  &lt;cfgman port="ccc" /&gt;               
                  &lt;webservice provider="ccc"  /&gt;
                  &lt;webservice provider="ccc"  /&gt;
                  &lt;webservice provider="ccc"  /&gt;
                      &lt;parameters&gt;
                          &lt;useeventpush value="ccc"/&gt;
                      &lt;/parameters&gt;
                  &lt;webservice provider="ccc"  /&gt;
                          &lt;pollingFrequency value="1000"/&gt;
          &lt;/DataGroup&gt;
      &lt;/DataPool&gt;
      &lt;DataGroup Id="ccc " UserCanSelectHost="ccc" &gt;
          &lt;DataGroup Id="ccc " UserCanSelectHost="ccc" &gt;
              &lt;HostId&gt;idk&lt;/HostId&gt;
              &lt;DataSources&gt;
                  &lt;empty/&gt;
              &lt;/DataSources&gt;
          &lt;/DataGroup&gt;
          &lt;DataGroup Id="ccc " UserCanSelectHost="ccc" &gt;
              &lt;HostId&gt;idk&lt;/HostId&gt;
              &lt;DataSources&gt;
                  &lt;empty/&gt;
              &lt;/DataSources&gt;
          &lt;/DataGroup&gt;
          &lt;DataGroup Id="default" UserCanSelectHost="true" &gt;
              &lt;HostId&gt;idk&lt;/HostId&gt;
          &lt;/DataGroup&gt;
      &lt;/DataGroup&gt;    
  &lt;/DataPools&gt;    
  &lt;/DataConfiguration&gt;
  </code></pre>