OpenXML 更新 customXMLPart

发布于 2024-10-04 04:04:27 字数 927 浏览 0 评论 0原文

我一直在使用 OOXML api 来更新 .docx 中的自定义 xml 部分。该代码更新文档中的自定义 xml 部分。我的问题是,当我在控制台应用程序中使用时,相同的代码会替换并生成完美的 .docx,但在 ASP.NET 应用程序中使用时,它不会替换也不会生成 .docx。有问题的代码片段如下:

 string tmp = string.Format("{0}.docx", Guid.NewGuid());
        File.Copy(FileName, tmp);

        _xml = ReadXML(XmlPath);
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(tmp, true)) {
            var mainPart = wordDoc.MainDocumentPart;

            mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts);

            //Add a new customXML part and then add content
            var customXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

            //copy the XML into the new part...
            using (var ts = new StreamWriter(customXmlPart.GetStream())) {
                ts.Write(_xml);
                ts.Flush();
            }
        }

我处于正方形,为什么会发生这种情况。任何帮助表示赞赏 谢谢

I have been using OOXML api to update the custom xml part in a .docx. The code updates the custom xml part in the document. My problem is that the same code replaces and generates perfect .docx when I use in a console App, but it doesn't replaces nor generates .docx when used in ASP.NET application. The code snippet in question is as follows:

 string tmp = string.Format("{0}.docx", Guid.NewGuid());
        File.Copy(FileName, tmp);

        _xml = ReadXML(XmlPath);
        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(tmp, true)) {
            var mainPart = wordDoc.MainDocumentPart;

            mainPart.DeleteParts<CustomXmlPart>(mainPart.CustomXmlParts);

            //Add a new customXML part and then add content
            var customXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

            //copy the XML into the new part...
            using (var ts = new StreamWriter(customXmlPart.GetStream())) {
                ts.Write(_xml);
                ts.Flush();
            }
        }

I am at square why is this happening. Any help is appreciated
Thanks

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

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

发布评论

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

评论(1

冷…雨湿花 2024-10-11 04:04:27

更好的方法可能是有选择地仅更新您有兴趣替换的 XML 部分。

检查我的答案这里基本上同样的问题。

在控制台中使用相同的代码后,我目前正在 MVC 应用程序中执行此操作,因此我不确定什么可能/会导致控制台/web.xml 之间出现问题。

WordProcessingDocument.Open() 的签名似乎也已更改,因此自动保存可能无法正常工作。另外,是否有可能抛出异常,而该异常以某种方式被调用者吞噬?也许将 try/catch 放入外部 using(...){...} 中。

A better approach to this is probably to selectively update only the XML Part you are interested in replacing.

Check my answer here to basically the same question.

I'm currently doing this in an MVC app after starting with the same code in console, so I'm not sure what could/would be causing a problem between console/web.

It also looks like the signature has changed for WordProcessingDocument.Open() so perhaps the AutoSave isn't working. Also, is it possible an exception is being thrown that is somehow being swallowed by the caller? Maybe toss a try/catch into the outer using(...){...}.

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