Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my custom XML not carry over to a new version of a DOCX file when Word saves it?

I'm adding in some custom XML to a docx for tracking it inside an application I'm writing.

I've manually done it via opening the Word Document via a ZIP library, and via the official Open XML SDK route. Both have the same outcome of my XML being inserted into customXml folder in the document. The document opens fine in Word for both of these methods, and the XML is present.

BUT when I then save the document as MyDoc2.docx for example all my XML disappears.

What am I doing wrong?

Microsoft links I've been following:

http://msdn.microsoft.com/en-us/library/bb608597.aspx
http://msdn.microsoft.com/en-us/library/bb608612.aspx

And the code I've taken from the Open XML SDK 2.0:

public static void AddNewPart(string document, string fileName)
{
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
    {
        MainDocumentPart mainPart = wordDoc.MainDocumentPart;

        CustomXmlPart myXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

        using (FileStream stream = new FileStream(fileName, FileMode.Open))
        {
            myXmlPart.FeedData(stream);
        }
    }
}

Thanks, John

like image 302
John Mc Avatar asked Jun 14 '11 09:06

John Mc


People also ask

Does DOCX use XML?

Starting with the 2007 Microsoft Office system, Microsoft Office uses the XML-based file formats, such as . docx, . xlsx, and . pptx.

Is Word XML the same as docx?

The Office Open XML-based word processing format using . docx as a file extension has been the default format produced for new documents by versions of Microsoft Word since Word 2007.

Is Office Open XML the same as docx?

The Open XML format (. docx/. xlsx/. pptx) is the default format in all supported versions of Microsoft Office and, unless you have a specific reason to use a different format, it's the format we recommend using for your Office files.

How do I see the XML of my docx document?

Double click the folder you wish to inspect (for example word). Double click the file you wish to inspect (for example document. xml). The document last selected should now appear in an Internet Explorer tab.


1 Answers

Ok,so I managed to find the following article Using Custom XML Part as DataStore on openxmldeveloper.org, and have stripped out the unnecessary code so that it inserts and retains custom XML:

static void Main(string[] args)
{
    using (WordprocessingDocument doc = WordprocessingDocument.Open("Test.docx", true, new OpenSettings()))
    {
        int customXmlPartsCount = doc.MainDocumentPart.GetPartsCountOfType<CustomXmlPart>();

        if (customXmlPartsCount == 0)
        {
            CustomXmlPart customXmlPersonDataSourcePart = doc.MainDocumentPart.AddNewPart<CustomXmlPart>("application/xml", null);
            using (FileStream stream = new FileStream("Test.xml", FileMode.Open))
            {
                customXmlPersonDataSourcePart.FeedData(stream);
            }


            CustomXmlPropertiesPart customXmlPersonPropertiesDataSourcePart = customXmlPersonDataSourcePart
                                                                              .AddNewPart<CustomXmlPropertiesPart>("Rd3c4172d526e4b2384ade4b889302c76");

            Ds.DataStoreItem dataStoreItem1 = new Ds.DataStoreItem() { ItemId = "{88e81a45-98c0-4d79-952a-e8203ce59aac}" };
            customXmlPersonPropertiesDataSourcePart.DataStoreItem = dataStoreItem1;
        }
    }
}

So all the examples from Microsoft work as long as you don't modify the file. The problem appears to be because we don't setup the relationship with the Main Document.

like image 84
John Mc Avatar answered Sep 28 '22 20:09

John Mc