title: LINQ and XML with C#
body: <pre><code>&lt;root&gt;
      &lt;level1 name="A"&gt;
          &lt;level2 name="A1" /&gt;
          &lt;level2 name="A2" /&gt;
      &lt;/level1&gt;
      &lt;level1 name="B"&gt;
          &lt;level2 name="B1" /&gt;
          &lt;level2 name="B2" /&gt;
      &lt;/level1&gt;
      &lt;level1 name="C" /&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>Could someone give me a C# code using LINQ, the simplest way to print this result:
  (Note the extra space if it is a level2 node)</p>
  
  <pre><code>A
    A1
    A2
  B
    B1
    B2
  C
  
  XDocument xdoc = XDocument.Load("data.xml"));
  var lv1s = from lv1 in xdoc.Descendants("level1")
             select lv1.Attribute("name").Value;
  
  foreach (var lv1 in lv1s)
  {
      result.AppendLine(lv1);
  
      var lv2s = from lv2 in xdoc...???
  }
  </code></pre>