title: How to merge 2 XML files with C#
body: <p>I have two XML files and I want to merge this two files to one.
  But how? I have tried a lot but nothing helps.
  As you can see the merged XML has left the text from the second attribute if it came from the first XML.
  Second the Element have to be ordered by there Id/Name/whatevername the first attribute have.
  Third if a node doesn't exist in XML 2 then it has to be created at the same place as in XML 1.</p>
  
  <p>The XML files showed here are just a fragment of the whole XML, there 're a lot more attributes names.</p>
  
  <p>How can I do this with C#?</p>
  
  <p>XML 1</p>
  
  <pre><code>&lt;APPLICATION&gt;
      &lt;AC&gt;
              &lt;CLASS Name="Hello1" Capt="do1"/&gt;
              &lt;CLASS Name="Hello2" Capt="do2"/&gt;
              &lt;CLASS Name="Hello5" Capt="do5"/&gt;
              &lt;CLASS Name="Hello8" Capt="do8"/&gt;
      &lt;/AC&gt;
  
      &lt;BO&gt;
              &lt;ITEM Id="1" DefaultValue="name1"/&gt;
              &lt;ITEM Id="3" DefaultValue="name3"/&gt;
              &lt;ITEM Id="11" DefaultValue="name11"/&gt;
              &lt;ITEM Id="12" DefaultValue="name12"&gt;
                      &lt;VAL&gt;
                              &lt;REASON Id="Job1" SecondOne="Hallo"/&gt;
                      &lt;/VAL&gt;
              &lt;/ITEM&gt;
      &lt;/BO&gt;
      &lt;POP Id="Green" Value="Monster"/&gt;
      &lt;POP Id="Blue" Value="Doggie"/&gt;
  </code></pre>
  
  <p></p>
  
  <p>XML 2 </p>
  
  <pre><code>&lt;APPLICATION&gt;
      &lt;AC&gt;
              &lt;CLASS Name="Hello1" Capt="dodo1"/&gt;
              &lt;CLASS Name="Hello2" Capt="dodo2"/&gt;
              &lt;CLASS Name="Hello3" Capt="dodo3"/&gt;
              &lt;CLASS Name="Hello9" Capt="dodo9"/&gt;
      &lt;/AC&gt;
      &lt;CARS Wheel="Fore" Default="45x255xZ"/&gt;
      &lt;CARS Wheel="BACK" Default="45x255xZ"/&gt;
      &lt;CARS Wheel="SPARE" Default="45x255xZ"/&gt;
      &lt;BO&gt;
              &lt;ITEM Id="1" DefaultValue="namename1"/&gt;
              &lt;ITEM Id="3" DefaultValue=""/&gt;
              &lt;ITEM Id="9" DefaultValue="name11"/&gt;
              &lt;ITEM Id="10" DefaultValue="name12"&gt;
                      &lt;VAL&gt;
                              &lt;REASON Id="Job1" SecondOne="Hallo"/&gt;
                      &lt;/VAL&gt;
              &lt;/ITEM&gt;
      &lt;/BO&gt;
  </code></pre>
  
  <p></p>
  
  <p>XML should look like this after merge:</p>
  
  <pre><code>&lt;APPLICATION&gt;
      &lt;AC&gt;
              &lt;CLASS Name="Hello1" Capt="dodo1"/&gt;
              &lt;CLASS Name="Hello2" Capt="dodo2"/&gt;
              &lt;CLASS Name="Hello3" Capt="dodo3"/&gt;
              &lt;CLASS Name="Hello5" Capt=""/&gt;
              &lt;CLASS Name="Hello8" Capt=""/&gt;
              &lt;CLASS Name="Hello9" Capt="dodo9"/&gt;
      &lt;/AC&gt;
      &lt;CARS Wheel="Fore" Default="45x255xZ"/&gt;
      &lt;CARS Wheel="BACK" Default="45x255xZ"/&gt;
      &lt;CARS Wheel="SPARE" Default="45x255xZ"/&gt;
      &lt;BO&gt;
              &lt;ITEM Id="1" DefaultValue="namename1"/&gt;
              &lt;ITEM Id="3" DefaultValue=""/&gt;
              &lt;ITEM Id="9" DefaultValue="name11"/&gt;
              &lt;ITEM Id="10" DefaultValue="name12"&gt;
                      &lt;VAL&gt;
                              &lt;REASON Id="Job1" SecondOne="Hallo"/&gt;
                      &lt;/VAL&gt;
              &lt;/ITEM&gt;
              &lt;ITEM Id="11" DefaultValue=""/&gt;
              &lt;ITEM Id="12" DefaultValue=""&gt;
                      &lt;VAL&gt;
                              &lt;REASON Id="Job1" SecondOne=""/&gt;
                      &lt;/VAL&gt;
              &lt;/ITEM&gt;
      &lt;/BO&gt;
      &lt;POP Id="Green" Value=""/&gt;
      &lt;POP Id="Blue" Value=""/&gt;
  </code></pre>
  
  <p></p>
  
  <p>Thanx for all the answers, but still I have the problem that I don't know how the tagnames are, so I cann't hardcode the tags.</p>
  
  <p>I just have to give you an example what it can look like. But the next time I get my XML files, the tags from above can be totally different. That is where the problem is. So I cann't say new XElement("BO", boChildren), because the next time this tag doesn't exist anymore.</p>
  
  <p>Or I cann't hardcode this ==> var cars = xDocuments.SelectMany(x => x.Root.Elements("CARS")).Merge(); because the next time I get my XML files "CARS" doesn't exist anymore.</p>