title: OpenXml Sdk - Copy Sections of docx into another docx
body: <p>I am trying the following code.  It takes a fileName (docx file with many sections) and I try to iterate through each section getting the section name.  The problem is that I end up with unreadable docx files.  It does not error, but I think I am doing something wrong with getting the elements in the section.</p>
  
  <pre><code>public void Split(string fileName) {
              using (WordprocessingDocument myDoc =
                  WordprocessingDocument.Open(fileName, true)) {
                  string curCliCode = "";
                  MainDocumentPart mdp = myDoc.MainDocumentPart;
  
                  foreach (var element in mdp.Document.Body.ChildElements) {
                      if (element.Descendants().OfType&lt;SectionProperties&gt;().Count() == 1) {
                          //get the name of the section from the footer
                          var footer = (FooterPart) mdp.GetPartById(
                                                        element.Descendants().OfType&lt;SectionProperties&gt;().First().OfType
                                                            &lt;FooterReference&gt;().First().
                                                            Id.Value);
                          foreach (Paragraph p in footer.Footer.ChildElements.OfType&lt;Paragraph&gt;()) {
                              if (p.InnerText != "") {
                                  curCliCode = p.InnerText;
                              }
                          }
                          if (curCliCode != "") {
                              var forFile = new List&lt;OpenXmlElement&gt;();
                              var els = element.ElementsBefore();
                              if (els != null) {
                                  foreach (var e in els) {
                                      if (e != null) {
                                          forFile.Add(e);
                                      }
                                  }
                                  for (int i = 0; i &lt; els.Count(); i++) {
                                      els.ElementAt(i).Remove();
                                  }
                              }
                              Create(curCliCode, forFile);
                          }
                      }
                  }
  
              }
          }
          private void Create(string cliCode,IEnumerable&lt;OpenXmlElement&gt; docParts) {
              var parts = from e in docParts select e.Clone();
              const string template = @"\Test\toSplit\blank.docx";
              string destination = string.Format(@"\Test\{0}.docx", cliCode);
              File.Copy(template, destination,true);
              /* Create the package and main document part */
              using (WordprocessingDocument myDoc =
                  WordprocessingDocument.Open(destination, true)) {
                  MainDocumentPart mainPart = myDoc.MainDocumentPart;
                  /* Create the contents */
                  foreach(var part in parts) {
                      mainPart.Document.Body.Append((OpenXmlElement)part);
                  }
  
                  /* Save the results and close */
                  mainPart.Document.Save();
                  myDoc.Close();
              }
          }
  </code></pre>
  
  <p>Does anyone know what the problem could be (or how to properly copy a section from one document to another)?</p>