title: Comparing two xmlfiles, adding missing elements
body: <p>Here comes a tricky question.</p>
  
  <p>I got one file, MainFile.XML that looks like this:</p>
  
  <pre><code>&lt;xml&gt;
    &lt;header&gt;
       &lt;some&gt;&lt;/some&gt;
       &lt;addThis&gt;&lt;/addThis&gt;
    &lt;/header&gt;
    &lt;footer&gt;&lt;/footer&gt;
    &lt;this&gt;
      &lt;is&gt;
        &lt;deep&gt;
          &lt;like&gt;&lt;/like&gt;
       &lt;/deep&gt;
     &lt;/is&gt;
    &lt;/this&gt;
  &lt;test&gt;&lt;/test&gt;
  &lt;page&gt;&lt;/page&gt;
  &lt;addThis&gt;&lt;/addThis&gt;
  </code></pre>
  
  <p></p>
  
  <p>And my other file, LangFile.XML looks like this.</p>
  
  <pre><code>&lt;xml&gt;
    &lt;header&gt;
      &lt;some&gt;English file&lt;/some&gt;
    &lt;/header&gt;
    &lt;footer&gt;Footer&lt;/footer&gt;
    &lt;this&gt;
      &lt;is&gt;
        &lt;deep&gt;
          &lt;like&gt;Hey&lt;/like&gt;
        &lt;/deep&gt;
      &lt;/is&gt;
    &lt;/this&gt;
    &lt;test&gt;Something&lt;/test&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p>I want to update my LangFile.XML so that it matches my MainFile.XML but I need to keep all Text values in the LangFile. </p>
  
  <p>I want the LangFile to look like this after an update:
  <strong>EXPECTED OUTPUT</strong></p>
  
  <pre><code>&lt;xml&gt;
    &lt;header&gt;
      &lt;some&gt;English file&lt;/some&gt;
      &lt;addThis&gt;&lt;/addThis&gt;
    &lt;/header&gt;
    &lt;footer&gt;Footer&lt;/footer&gt;
    &lt;this&gt;
    &lt;is&gt;
      &lt;deep&gt;
        &lt;like&gt;Hey&lt;/like&gt;
      &lt;/deep&gt;
    &lt;/is&gt;
    &lt;/this&gt;
    &lt;test&gt;Something&lt;/test&gt;
    &lt;page&gt;&lt;/page&gt;
    &lt;addThis&gt;&lt;/addThis&gt;
  &lt;/xml&gt;
  </code></pre>
  
  <p>I've looked at this answer but I need to update the file and keep the values...
  <a href="https://stackoverflow.com/questions/14370070/compare-two-text-files-line-by-line">Compare two text files line by line</a></p>
  
  <p>The tricky part is the nesting, it can be anything between 1 level up to X levels deep...</p>
  
  <p>My problem is that I dont know how to compare the rows row by row when going deeper in the tree, I've tried something like this but im stuck... I dont know how to add the specific descendant to the new list.</p>
  
  <pre><code>String directory = @"C:\Utv\XmlTest";
  
  var mainFile = XDocument.Load(Path.Combine(directory, "MainFile.XML"));
  var langFile = XDocument.Load(Path.Combine(directory, "LangFile.XML"));
  
  //Get all descendant nodes
  var mainFileDesc = mainFile.Root.Descendants().ToList();
  var langFileDesc = langFile.Root.Descendants().ToList();
  
  //Loop through the mainfile
  for(var i = 0; i &lt; mainFileDesc.Count(); i++)
  {
      var mainRow = mainFileDesc[i];
      var langRow = langFileDesc[i];
  
      //Compare the rows descendants, if not the same, add the mainRow to the langRow
      if(mainRow.Descendants().Count() != langRow.Descendants().Count())
      {
          //Here I want to check if the mainRow != the langRow
                      //if not, add the mainRow to the langFile list
                      if(mainRow != langRow)
                      {
                        langFileDesc.Insert(i, mainRow);
                      }
      }
  }
  </code></pre>
  
  <p>Im getting the following error now: </p>
  
  <pre><code>var langRow = langFileDesc[i];
  Message Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index 
  </code></pre>
  
  <p>That's because the lists doesn't have the same length, that's why I need to add it to the list...</p>