title: Adding child nodes to an XElement
body: <p>I am trying to append objects into an XML file. The problem I currently have is it appends everything at the first level itself. I am trying to have the <strong>list as the parent element and list items as the child elements</strong>. </p>
  
  <p><em>What I've tried</em>: I came across a few posts where they use loops but I am unable to relate that to my context and code.</p>
  
  <p>Code:</p>
  
  <pre><code>XDocument xDocument = XDocument.Load(@"C:\Users\hci\Desktop\Nazish\TangramsTool\TangramsTool\patterndata.xml");
  XElement root = xDocument.Element("Patterns");
  foreach (Pattern currentPattern in PatternDictionary.Values)
  {
       String filePath = currentPattern.Name.ToString();
       IEnumerable&lt;XElement&gt; rows = root.Descendants("Pattern"); // Returns a collection of the descendant elements for this document or element, in document order.
       XElement firstRow = rows.First(); // Returns the first element of a sequence.
       if (currentPattern.PatternDistancesList.Count() == 9)
       {
             firstRow.AddBeforeSelf( //Adds the specified content immediately before this node.
             new XElement("Pattern"),
             new XElement("Name", filePath.Substring(64)),
             new XElement("PatternDistancesList"),
             new XElement("PatternDistance", currentPattern.PatternDistancesList[0].ToString()),
             new XElement("PatternDistance", currentPattern.PatternDistancesList[1].ToString()),
       }
  }
  </code></pre>
  
  <p>Current XML File:</p>
  
  <pre><code>&lt;Pattern/&gt; 
  &lt;Name&gt;match.jpg&lt;/Name&gt; 
  &lt;PatternDistancesList/&gt;       
  &lt;PatternDistance&gt;278&lt;/PatternDistance&gt;
  &lt;PatternDistance&gt;380&lt;/PatternDistance&gt; 
  </code></pre>
  
  <p>What I would like as the end result:</p>
  
  <pre><code>&lt;Pattern&gt; 
  &lt;Name&gt;match.jpg&lt;/Name&gt; 
  &lt;PatternDistancesList&gt;       
      &lt;PatternDistance&gt;278&lt;/PatternDistance&gt;
      &lt;PatternDistance&gt;380&lt;/PatternDistance&gt;
  &lt;/PatternDistancesList&gt; 
  &lt;Pattern/&gt;
  </code></pre>
  
  <p>Any tips will be much appreciated. I'm new to WPF and C# so still trying to learn things. </p>