Why replacing the value of xml node in custom xml part in word 2013 document makes it corrupt?

1.1k Views Asked by At

I created a Word 2013 document and did the following:

  1. Added a Plain Text Content Control to it at design time.
  2. Added a Custom Xml Part at design time.
  3. Did a mapping from one node of Custom Xml Part to the Plain Text Content Control.
  4. The value of node appeared in the Content control.
  5. I saved and closed the document.
  6. Renamed it to .zip and extracted it to a folder.
  7. Edited the file in folder customXml/item1.xml which is my custom xml part and changed the value of node from <Name>John</Name> to <Name>Harry</Name>.
  8. Re archived it as a zip file and renamed it to .docx.
  9. When I opened the document, it was corrupted and Microsoft Word says:-

We're sorry. We can't open XYZ.docx because we found a problem with the contents. Microsoft Office cannot open this file because some parts are missing or invalid.

2

There are 2 best solutions below

0
On BEST ANSWER

I was zipping and unzipping incorrectly. We don't need any Open XML SDK.

What I was doing was: Right click on XYZ.docx.zip and select Extract to XYZ.docx. When it got extracted into the folder XYZ.docx, I edited the contents inside and then zipped the entire folder to XYZ.docx which is wrong.

When I got inside of folder and zipped only the contents to XYZ.docx.zip, it started working.

0
On

Reason - You cannot unzip and re-zip your .docx file as you did in Step 8

Guide - Try this. Create a dummy .docx file form word. Extract it and re-zip it and try to open the file. You will get the same error and you will not be able to use it as you expect.

Solution - If you want to edit your .docx file , use Open XML SDK and do it. Here is a link to a good guide - http://msdn.microsoft.com/EN-US/library/office/cc850833(v=office.15).aspx . And also Open XML productivity tool will come in handy - http://dotnet.dzone.com/articles/using-openxml-sdk-productivity