为什么当 Word 保存自定义 XML 时,它没有保留到新版本的 DOCX 文件中?

发布于 2024-11-15 11:23:51 字数 1105 浏览 2 评论 0原文

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

我通过 ZIP 库打开 Word 文档并通过官方 Open XML SDK 路径手动完成此操作。两者的结果相同,即我的 XML 被插入到文档中的 customXml 文件夹中。对于这两种方法,文档在 Word 中都可以正常打开,并且 XML 也存在。

但是,当我将文档另存为 MyDoc2.docx 时,我的所有 XML 都会消失。

我做错了什么?

我一直关注的 Microsoft 链接:

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

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

谢谢, 约翰

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Oo萌小芽oO 2024-11-22 11:23:51

好的,所以我设法找到以下文章 使用自定义 XML 部件作为数据存储 在 openxmldeveloper.org 上,并删除了不必要的代码,以便插入并保留自定义 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 的所有示例都可以工作。问题似乎是因为我们没有设置与主文档的关系。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文