使用 Word 的 CustomXMLPart 或任何其他支持的方式存储 UTF-8 XML
我正在编写一个 Word 加载项,它应该使用 Word 对象模型及其 CustomXMLPart
为每个文档存储一些自己的 XML 数据。我现在面临的问题是缺乏类似 IStream
的功能来从 CustomXMLPart
读取 XML 或将 XML 写入其中。它只提供 BSTR
接口,我很困惑如何使用 BSTR
处理 UTF-8 XML。据我了解,UTF-8 XML 文件实际上永远不需要经历这种 Unicode 转换。我不确定这里会产生什么结果。
是否有另一种方法使用 Word 自动化界面在 DOCX 文件中存储任意自定义信息?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“包”是一个 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.