Modifying Word Document XML using Packages

HellsBells

New Member
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.I have a basic test document with two placeholders mapped to the following XML: \[code\]<root> <element> Fubar </element> <second> This is the second placeholder </second></root>\[/code\]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. Here is the code I am using: \[code\]string strRelRoot = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";byte[] buffer = File.ReadAllBytes("dev.docx");//stream with the templateMemoryStream stream = new MemoryStream(buffer, true);//create a package using the streamPackage package = Package.Open(stream, FileMode.Open, FileAccess.ReadWrite);PackageRelationshipCollection pkgrcOfficeDocument = package.GetRelationshipsByType(strRelRoot);foreach (PackageRelationship pkgr in pkgrcOfficeDocument){ if (pkgr.SourceUri.OriginalString == "/") { Uri uriData = http://stackoverflow.com/questions/11434236/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 = http://stackoverflow.com/questions/11434236/package.CreatePart(uriData,"application/xml"); //test data string xml = @"<root> <element> Changed </element> <second> The second placeholder changed </second> </root>"; //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\]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!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.
 
Back
Top