开发者

OpenXML updating customXMLPart

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.Fo开发者_Python百科rmat("{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


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(...){...}.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜