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