title: LINQ to XML: how do I add child elements in a for-each loop?
body: <pre><code>Dim names() As String = {"one", "two", "three"}
  Dim xml As XElement = Nothing
  For Each name In names
    If xml Is Nothing Then
      xml = New XElement(name)
    Else
      xml.Add(New XElement(name)
    End If
  Next
  </code></pre>
  
  <p>The above code will create something like this:</p>
  
  <pre><code>  &lt;One&gt;
      &lt;Two /&gt;
      &lt;Three /&gt;
    &lt;/One&gt;
  </code></pre>
  
  <p>What I need is something like this:</p>
  
  <pre><code>  &lt;One&gt;
      &lt;Two&gt;
        &lt;Three /&gt;
      &lt;/Two&gt;
    &lt;/One&gt;
  </code></pre>
  
  <p>I tried using <code>xml.Elements.Last.Add(New XElement(name))</code>, but the <code>Last</code> method isn't necessarily returning the last element for some reason.</p>
  
  <p>Thanks!</p>