title: Merge multiple word documents into one using OpenXML and XElement
body: <p>As the title states I am trying to merge multiple word(.docx) files into one word doc. Each of these documents is one page long. I am using some of the code from <a href="https://stackoverflow.com/questions/247666/how-can-i-programatically-use-c-sharp-to-append-multiple-docx-files-together">this post</a> in this implementation. The issue I am running into is that only the first document gets written properly, every other iteration appends a new document but the document contents is the same as the first.</p>
  
  <p>Here is the code I am using:</p>
  
  <pre><code>//list that holds the file paths
  List&lt;String&gt; fileNames = new List&lt;string&gt;();
  fileNames.Add("filePath");
  fileNames.Add("filePath");
  fileNames.Add("filePath");
  fileNames.Add("filePath");
  fileNames.Add("filePath");
  
  //get the first document
  MemoryStream mainStream = new MemoryStream();
  byte[] buffer = File.ReadAllBytes(fileNames[0]);
  mainStream.Write(buffer, 0, buffer.Length);
  
  using (WordprocessingDocument mainDocument = WordprocessingDocument.Open(mainStream, true))
  {
      //xml for the new document
      XElement newBody = XElement.Parse(mainDocument.MainDocumentPart.Document.Body.OuterXml);
      //iterate through eacah file
      for (int i = 1; i &lt; fileNames.Count; i++)
      {
          //read in the document
          byte[] tempBuffer = File.ReadAllBytes(fileNames[i]);
          WordprocessingDocument tempDocument = WordprocessingDocument.Open(new MemoryStream(tempBuffer), true);
          //new documents XML
          XElement tempBody = XElement.Parse(tempDocument.MainDocumentPart.Document.Body.OuterXml);
          //add the new xml
          newBody.Add(tempBody);
          string str = newBody.ToString();
          //write to the main document and save
          mainDocument.MainDocumentPart.Document.Body = new Body(newBody.ToString());
          mainDocument.MainDocumentPart.Document.Save();
          mainDocument.Package.Flush();
          tempBuffer = null;
      }
      //write entire stream to new file
      FileStream fileStream = new FileStream("xmltest.docx", FileMode.Create);
      mainStream.WriteTo(fileStream);
      //ret = mainStream.ToArray();
      mainStream.Close();
      mainStream.Dispose();
  }
  </code></pre>
  
  <p>Again the problem is that each new document being created has the same content as the first document. So when I run this the output will be a document with five identical pages. I've tried switching the documents order around in the list and get the same result so it is nothing specific to one document. 
  Could anyone suggest what I am doing wrong here? I'm looking through it and I can't explain the behavior I am seeing. Any suggestions would be appreciated. Thanks much!</p>
  
  <p><strong>Edit:</strong> I'm thinking this may have something to do with that fact that the documents I am trying to merge have been generated with custom XML parts. I'm thinking that the Xpath in the documents are somehow pointing to the same content. The thing is I can open each of these documents and see the proper content, it's just when I merge them that I see the issue. </p>