当Word保存它时,为什么我的自定义XML不会继续传送到DOCX文件的新版本? [英] Why does my custom XML not carry over to a new version of a DOCX file when Word saves it?

查看:157
本文介绍了当Word保存它时,为什么我的自定义XML不会继续传送到DOCX文件的新版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向docx中添加一些自定义XML,以便在我正在编写的应用程序中跟踪它.

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

我已经通过ZIP库和正式的Open XML SDK路线打开Word文档来手动完成此操作.两者都具有将我的XML插入文档中的customXml文件夹中的相同结果.对于这两种方法,该文档都可以在Word中很好地打开,并且存在XML.

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.

但是当我然后将文档另存为MyDoc2.docx时,例如我所有的XML都消失了.

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

我在做什么错了?

我一直关注的Microsoft链接:

Microsoft links I've been following:

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

还有我从Open XML SDK 2.0中获取的代码:

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);
        }
    }
}

谢谢, 约翰

推荐答案

好,所以我设法找到了以下文章

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;
        }
    }
}

因此,只要您不修改文件,Microsoft的所有示例都可以使用.问题似乎是因为我们没有与主文档建立关系.

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.

这篇关于当Word保存它时,为什么我的自定义XML不会继续传送到DOCX文件的新版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆