title: Modifying Word Document XML using Packages
body: <p>I am attempting to modify a simple MS word templates XML. I realize there are SDK's available that could make this process easier but what I am tasked with maintaining uses packages and I was told to do the same.</p>
  
  <p>I have a basic test document with two placeholders mapped to the following XML: </p>
  
  <pre><code>&lt;root&gt;
    &lt;element&gt;
       Fubar
    &lt;/element&gt;
    &lt;second&gt;
       This is the second placeholder
    &lt;/second&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>What I am doing is creating a stream using the word doc, removing the existing XML, getting some hard coded test XML and trying to write that to the stream. </p>
  
  <p>Here is the code I am using: </p>
  
  <pre><code>string strRelRoot = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
  byte[] buffer = File.ReadAllBytes("dev.docx");
  //stream with the template
  MemoryStream stream = new MemoryStream(buffer, true);
  //create a package using the stream
  Package package = Package.Open(stream, FileMode.Open, FileAccess.ReadWrite);
  PackageRelationshipCollection pkgrcOfficeDocument = package.GetRelationshipsByType(strRelRoot);
  foreach (PackageRelationship pkgr in pkgrcOfficeDocument)
  {
      if (pkgr.SourceUri.OriginalString == "/")
      {
          Uri uriData = new Uri("/customXML/item1.xml", UriKind.Relative);
          //remove the existing part
          if (package.PartExists(uriData))
          { 
              // Delete template "/customXML/item1.xml" part
              package.DeletePart(uriData);
          }
          //create a new part
          PackagePart pkgprtData = package.CreatePart(uriData, "application/xml");
          //test data
          string xml = @"&lt;root&gt;
                          &lt;element&gt;
                              Changed
                          &lt;/element&gt;
                          &lt;second&gt;
                                  The second placeholder changed
                          &lt;/second&gt;
                      &lt;/root&gt;";
          //stream created from the xml string
          MemoryStream fromStream = new MemoryStream();
          UnicodeEncoding uniEncoding = new UnicodeEncoding();
          byte[] fromBuffer = uniEncoding.GetBytes(xml);
          fromStream.Write(fromBuffer, 0, fromBuffer.Length);
          fromStream.Seek(0L, SeekOrigin.Begin);
          Stream toStream = pkgprtData.GetStream();
          //copy the xml to the part stream
          fromStream.CopyTo(toStream);
          //copy part stream to the byte stream
          toStream.CopyTo(stream);
  
      }
  }
  </code></pre>
  
  <p>This is currently not modifying the document although I feel like I am close to a solution. Any advice would be very much appreciated. Thanks!</p>
  
  <p>Edit: To clairify, the result I am getting is the document is unchanged. I get no exceptions or the like, but the documents XML is not modified. </p>