none
Force refresh or render document body after change custom xml part using WordProcessingDocument in c# RRS feed

  • Question

  • I created a docx document using an XML as a template. This XML contains only a record. Then, using C# and WordProcessingDocument I change the custom XML part, replacing it with an XML with the same structure but with various records. If I open the document with local Word (Office 365 version) it shows it rendered with all the records, but when I open the same document in Word OnLine it shows the "orignal" version of the document. But if I save the document opened in local Word and then I open it in Office OnLine it shows the correct version.

    I decompressed the docx files (before and after saving on local Word) and compared all the files. I realized that the main document XML is not refreshed with the new content from the custom XML part I've changed.

    This is the code I'm using to do it:

        static void TestCustomXMLParts()
        {
            string sourcePath = @"C:\WordTest\template.docx";
            string destinationPath = @"C:\WordTest\document.docx";
            string xmlPath = @"C:\WordTest\data.xml";
            File.Copy(sourcePath, destinationPath, true);
            using (WordprocessingDocument document = WordprocessingDocument.Open(destinationPath, true))
            {
                MainDocumentPart mainPart = document.MainDocumentPart;
                var xmlPart = mainPart.CustomXmlParts.First();
                using (FileStream stream = new FileStream(xmlPath, FileMode.Open))
                {
                    xmlPart.FeedData(stream);
                }
                document.Save();
            }
        }

    Is there any way to refresh or render again the document using the OpenXML C# library?

    • Moved by woshixiaobai Tuesday, July 9, 2019 2:39 AM move this case to the right forum
    Monday, July 8, 2019 5:33 AM

All replies

  • Hi zhodges,

    Thanks for visiting our forum. Then here is mainly focus on troubleshooting issues regarding Teams. Based on your description, you have some issue with Word develop, so I move this case to the Word for Developers forum. 
    Thanks for your understanding.

    Best Regards,
    Evan Jiang


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, July 9, 2019 2:39 AM
  • The Open XML SDK doesn't provide anything for that.

    > But if I save the document opened in local Word and then I open it in Office OnLine it shows the correct version.

    It seems you need to re-load the document or refresh the web page with a document opened in word online.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Tuesday, July 9, 2019 10:24 AM