开发者

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 (W开发者_如何学运维ordprocessingDocument 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜