title: I want to recursively list the nodes in xml
body: <p>I recursively want to display xml nodes. But unfortunately it doesn't work. The output is only the first element of the xml file. Why?</p>
  
  <pre><code>public string GetOutline(int indentLevel, XmlNode xnod)
  {
          StringBuilder result = new StringBuilder();
          XmlNode xnodWorking;
  
          result = result.AppendLine(new string('-', indentLevel * 2) + xnod.Name);
  
          if (xnod.NodeType == XmlNodeType.Element)
          {
              if (xnod.HasChildNodes)
              {
                  xnodWorking = xnod.FirstChild;
                  while (xnodWorking != null)
                  {
                      GetOutline(indentLevel + 1, xnodWorking);
                      xnodWorking = xnodWorking.NextSibling;
  
                  }
              }
  
          }
          return result.ToString();
  }
  </code></pre>
  
  <p>Here the code calling the function. The XML file begins with <code>&lt;Videos&gt;</code> then <code>&lt;Video&gt;</code>... etc... </p>
  
  <pre><code>private void button2_Click(object sender, EventArgs e)
  {
          SaveFileDialog fDialog = new SaveFileDialog();
          fDialog.Title = "Save XML File";
          fDialog.FileName = "drzewo.xml";
          fDialog.CheckFileExists = false;
          fDialog.InitialDirectory = @"C:\Users\Piotrek\Desktop";
  
          if (fDialog.ShowDialog() == DialogResult.OK)
          {
              using (var newXmlFile = File.Create(fDialog.FileName));
              {
                  string xmlTree = fDialog.FileName.ToString();
  
                  XmlDocument xdoc = new XmlDocument();
                  xdoc.Load(XML);
  
                  XmlNode xnodDE = xdoc.DocumentElement;
  
                  textBox2.Text = GetOutline(0, xnodDE);
  
                  //StringBuilder result = new StringBuilder();
  
                  /*
                  foreach (var childelement in xdoc.DescendantNodes().OfType&lt;XElement&gt;()
                      .Select(x =&gt; x.Name).Distinct()) 
                  {
                     result.Append(childelement + Environment.NewLine );
  
                  }
                  textBox2.Text = result.ToString();
                  */
  
                  using (StreamWriter sw = File.AppendText(xmlTree))
                  {
                      sw.Write(textBox2.Text);
                  }
              }
  }
  </code></pre>
  
  <p>XML content : </p>
  
  <pre><code>&lt;Videos&gt;
    &lt;Video&gt;
      &lt;Title&gt;The Distinguished Gentleman&lt;/Title&gt;
      &lt;Director&gt;Jonathan Lynn&lt;/Director&gt;
      &lt;Actors&gt;
        &lt;Actor&gt;Eddie Murphy&lt;/Actor&gt;
        &lt;Actor&gt;Lane Smith&lt;/Actor&gt;
        &lt;Actor&gt;Sheryl Lee Ralph&lt;/Actor&gt;
        &lt;Actor&gt;Joe Don Baker&lt;/Actor&gt;
      &lt;/Actors&gt;
      &lt;Length&gt;112 Minutes&lt;/Length&gt;
      &lt;Format&gt;DVD&lt;/Format&gt;
      &lt;Rating&gt;R&lt;/Rating&gt;
    &lt;/Video&gt;
    &lt;Video&gt;
      &lt;Title&gt;Her Alibi&lt;/Title&gt;
      &lt;Director&gt;Bruce Beresford&lt;/Director&gt;
      &lt;Length&gt;94 Mins&lt;/Length&gt;
      &lt;Format&gt;DVD&lt;/Format&gt;
      &lt;Rating&gt;PG-13&lt;/Rating&gt;
    &lt;/Video&gt;
  &lt;/Videos&gt;
  </code></pre>