none
Update content of Custom XML

    Question

  • Hello All,

    I inserted a Microsoft.Office.Core.CustomXMLPart object to my document already. I want to replace another XML content to this Custom XML. I try to call CustomXMLPart.LoadXML method again, but I catch an exception "This custom XML part has already been loaded". Are there any ways to do this quickly? 

    Thanks for your helps.

    Regards,

    Vu

    Wednesday, May 04, 2011 9:50 AM

Answers

  • Hi Silviu,

    Because I don't know which nodes are updated, inserted or deleted, I have to replace everything to Custom XML. I found a solution to do. My code is below.

     

    for (int i = 1; i <= _customXML.DocumentElement.ChildNodes.Count; i++)

    {

         CustomXMLNode customXMLChild = _customXML.DocumentElement.ChildNodes[i];

         XmlNode updatedChild = xml.ChildNodes[i - 1];

     

         _customXML.DocumentElement.ReplaceChildSubtree(updatedChild.OuterXml, customXMLChild);

    }

     

    Thanks for your ideas.

    Regards,

    Vu

    • Marked as answer by ttvu Thursday, May 05, 2011 3:33 AM
    Thursday, May 05, 2011 3:33 AM

All replies

  • Hello,

     

    If you know the struture of your CustomXML, it is simple. Something like this:

            foreach (CustomXMLPart customXMLPart in Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts)
            {
              if (customXMLPart.DocumentElement.BaseName.Equals(ElementName))
              {
                foreach (CustomXMLNode cell in customXMLPart.DocumentElement.FirstChild.ChildNodes)
                {
                  if (cell.BaseName.Equals(CellName))
                  {
                    cell.Text = cellValue;
                    return;
                  }
                }          
              }
            }
    
    
    //in this case my structure is: ElementName->Cells
    

    Hope this helps,

    Silviu.


    http://www.rosoftlab.net/
    Wednesday, May 04, 2011 10:41 AM
  • Thanks for your help, Silviu.

    However, my custom XML is more complex. In addition, there are some nodes are deleted or inserted in new XML.

    Wednesday, May 04, 2011 10:58 AM
  • Hello ttvu,

     

    If you have created them, then try using this code:

     

          var myNode= Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts.SelectByID("myID");
    myNode.DocumentElement.Text = "new text";

     

    This supposedly you know (and given when you created it) the ID of the XML node.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Wednesday, May 04, 2011 11:16 AM
  • Hi Silviu,

    Because I don't know which nodes are updated, inserted or deleted, I have to replace everything to Custom XML. I found a solution to do. My code is below.

     

    for (int i = 1; i <= _customXML.DocumentElement.ChildNodes.Count; i++)

    {

         CustomXMLNode customXMLChild = _customXML.DocumentElement.ChildNodes[i];

         XmlNode updatedChild = xml.ChildNodes[i - 1];

     

         _customXML.DocumentElement.ReplaceChildSubtree(updatedChild.OuterXml, customXMLChild);

    }

     

    Thanks for your ideas.

    Regards,

    Vu

    • Marked as answer by ttvu Thursday, May 05, 2011 3:33 AM
    Thursday, May 05, 2011 3:33 AM