none
Replace XElement value within custom XML part RRS feed

  • Question

  • Hi Guys

    I'd like to know if it would be possible to edit an XML element (XElement) within a CustomXmlPart.

    i.e. I have an xml - 

    <heading xmlns="http://nameSpace.com">
    <subHeading>
    <Value>
    <Item>
    <Fullname>XNameX</Fullname>
    <Dateofbirth>XDateX</Dateofbirth>
    </Item>
    <Item>
    <Fullname>YNameY</Fullname>
    <Dateofbirth/>
    </Item>
    </Value>
    <Formats>
    <FullnameFormat="null">
    </Fullname>
    <DateofbirthFormat="null">
    </Dateofbirth>
    </Formats>
    </subHeading>
    </heading>


    I would like to grab a section (we can use <Formats> as its simple) as an XElement and replace it with a new XElement, then save it back to the current CustomXmlPart.

    I can change an attribute easier (by using the text), but I have not been able to grab a section of XML out (as XElement) then replace and save an updated XElement in its place.

    I can successfully grab the XML section I'm looking for, but I cannot change or save it back?

    I have tried changing NodeValue as well as the (ReadOnly) XML property - obviously without any luck.

    Thanks in advance.


    • Edited by Phsyke18 Thursday, September 1, 2016 5:08 AM update
    Thursday, September 1, 2016 5:05 AM

Answers

  • >>I can successfully grab the XML section I'm looking for, but I cannot change or save it back?

    To replace the XML node, you could use CustomXMLNode.ReplaceChildSubtree method

       

         // get the custom xml part
                CustomXMLParts xmlParts = Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts.SelectByNamespace(http://nameSpace.com);
                // get the child node you want to replace, we change <Formats> here
                var csns1 = xmlParts[1].DocumentElement.ChildNodes[1].ChildNodes[2];
                // dim new XML string
                string replaceString =
                    "<Formats xmlns=\"http://nameSpace.com\">" +
                    "<Fullname Format=\"test\">"+
                    "</Fullname>"+
                    "<Dateofbirth Format=\"test\">" +
                    "</Dateofbirth>" +
                    "</Formats>";
                // ReplaceChildSubtree
                xmlParts[1].DocumentElement.ChildNodes[1].ReplaceChildSubtree(replaceString, csns1);


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Click HERE to participate the survey.


    Friday, September 2, 2016 2:30 AM
    Moderator