Reputation: 73
I'm trying to edit a CustomXmlPart but I don't know how.
I tried this:
CustomXMLParts xmlParts = Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts.SelectByNamespace(@"MyNamespace");
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlParts[1].XML);
foreach (XmlNode mainNode in xmlDocument.ChildNodes)
{
foreach (XmlNode node in mainNode)
{
switch (node.LocalName)
{
case ("SelAdrIndex"):
node.InnerXml = "1111";
break;
}
}
}
But it's not working
The only other way I know, is to delete the XML part and add the edit version.
Upvotes: 1
Views: 533
Reputation: 25663
The properties and methods of the CustomXMLPart
object provide the capability to directly manipulate the content of a Custom XML Part. There is no need to use a save method or anything like that - the manipulation takes place immediately inside the XML file.
Note that the XML capabilities mirror those of the COM MSXML Parser and not the libraries of the .NET Framework.
Example to locate a node or nodes, then read/write the data.
private void btnEditCXP_Click(object sender, RibbonControlEventArgs e)
{
Word.Document doc = Globals.ThisAddIn.app.ActiveDocument;
string sXML = "<?xml version='1.0' encoding='utf-8'?><books><book><title>Test 1</title><author>Me</author></book></books>";
Office.CustomXMLPart cxp = doc.CustomXMLParts.Add(sXML);
Office.CustomXMLNodes nds = cxp.SelectNodes("//book");
System.Diagnostics.Debug.Print(nds.Count.ToString());
foreach (Office.CustomXMLNode nd in nds)
{
Office.CustomXMLNode ndTitle = nd.SelectSingleNode("//title");
System.Diagnostics.Debug.Print(ndTitle.Text);
ndTitle.Text = "11111";
System.Diagnostics.Debug.Print(ndTitle.Text);
}
}
Upvotes: 1