Update CustomXML Item1 RRS feed

  • Question

  • I have found the code below on this forum. I have successfully implemented it into my program, however it is not quite what I want.

    Is their a way that I can stream in the below string rather than create it from a document. This seams like it should be easy but
    I am struggling.

           string newXml = "<root>" +
                        "<AccountInfo>" +
                        "<Account>" + txtAccount.Text + "</Account> " +
                        "<Title>" + txtTitle.Text + "</Title>" +
                        "<Proposal>" + txtProposal.Text + "</Propsoal>" +
                        "</AccountInfo>" +

    publicstaticvoid UpdateCustomXML() { string templatePath = @"D:\EmployeeControls.docx"; string custXMLFilePath = @"D:\item2.xml"; using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(templatePath, true)) { MainDocumentPart mainPart = wdDoc.MainDocumentPart; // load custom XML XmlDataDocument custXML = new XmlDataDocument(); custXML.Load(custXMLFilePath); // get the XML part that needs to be updated CustomXmlPart custXMLPart = mainPart.CustomXmlParts.ElementAtOrDefault(1); // update the content of the custom XML partusing (Stream myStream = custXMLPart.GetStream(FileMode.Create)) { custXML.Save(myStream); } } }

    Monday, February 13, 2012 9:10 PM



    I think you need add the CustomXmlPart use following snippet:

    CustomXmlPart customXmlPart2 = mainDocumentPart1.AddNewPart<CustomXmlPart>("application/xml", "rId1");

    Please try it.

    Have a good day,


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, February 15, 2012 7:31 AM