使用 Word 的 CustomXMLPart 或任何其他支持的方式存储 UTF-8 XML

发布于 2024-09-04 00:51:44 字数 347 浏览 1 评论 0 原文

我正在编写一个 Word 加载项,它应该使用 Word 对象模型及其 CustomXMLPart 为每个文档存储一些自己的 XML 数据。我现在面临的问题是缺乏类似 IStream 的功能来从 CustomXMLPart 读取 XML 或将 XML 写入其中。它只提供 BSTR 接口,我很困惑如何使用 BSTR 处理 UTF-8 XML。据我了解,UTF-8 XML 文件实际上永远不需要经历这种 Unicode 转换。我不确定这里会产生什么结果。

是否有另一种方法使用 Word 自动化界面在 DOCX 文件中存储任意自定义信息?

I am writing a Word add-in which is supposed to store some own XML data per document using Word object model and its CustomXMLPart. The problem I am now facing is the lack of IStream-like functionality for reading/writing XML to/from a CustomXMLPart. It only provides BSTR interface and I am puzzled how to handle UTF-8 XMLs with BSTRs. To my understanding an UTF-8 XML file should really never have to undergo this sort of Unicode conversion. I am not sure what to expect as a result here.

Is there another way of using Word automation interfaces to store arbitrary custom information inside a DOCX file?

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

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

发布评论

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

评论(1

因为看清所以看轻 2024-09-11 00:51:44

“包”是一个 OPC 文档 (开放打包约定),它基本上是一个具有不同扩展名的结构化 zip 文件夹(例如 .pptx、.docx、.xps 等)。您可以在流中获取该文件并以任何您喜欢的方式操作它 - 但不是任意的。如果您将内容放在错误的位置(不仅是 xml 元素,还包括 zip 文件内的文件夹中的文件),它将不会被识别为有效的 docx。但如果您只是谈论“任意”,即 CustomXMLPart,那么也没关系。

这是一个很好的启动页面,可了解有关 开放 XML SDK,如果您愿意的话,与使用 (.NET) System.IO.Packaging 或第三方 zip 库相比,它可以更轻松地访问文件格式。要深入了解,请获取电子书(免费) 开放 XML 解释

使用 .NET 中的 Open XML SDK(同样,这一切都可以在没有 SDK 的情况下完成),这就是您想要做的:如何:使用 Open XML API 将自定义 XML 插入 Office Open XML 包

The "package" is an OPC document (Open Packaging Convention), which is basically a structured zip folder with a different extension (e.g. .pptx, .docx, .xps, etc.). You can get that file in stream and manipulate it any which way you like - but not artibitrarily. It will not be recognized as valid docx if you put things in the wrong places (not just xml elements, but also files in the folders inside the zip file). But if you're just talking "artibitrary" meaning CustomXMLPart, then that's okay.

This is a good kicker page to learn more about the Open XML SDK and if you're up to it, which allows for somewhat easier access to the file formats than using (.NET) System.IO.Packaging or a third-party zip library. To go deeper, grab the eBook (free) Open XML Explained.

With the Open XML SDK (again, this can all be done without the SDK) in .NET, this is what you'll want to do: How to: Insert Custom XML to an Office Open XML Package by Using the Open XML API.

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