Kman Kman - 1 month ago 22
C# Question

Word VSTO CustomXMLNode.SelectSingleNode fails after Office upgrade

A word vsto addin fails with exception on calling CustomXMLNode.SelectSingleNode.

The addin runs fine with word 2007 - 2013, but on version 2016 it fails with Reference to undeclared namespace prefix: 'ns0'.

visual studio screenshot

I've made a workaround navigating the XML without the use of xpath and these methods, but still need to figure out how to solve this issue.

var xPathExpression = String.Format(
"{0}:{1}[1]",
customXml.NamespaceManager.LookupPrefix(xmlRef.DefaultNamespace),
xmlRef.ElementNames.DisplayText.LocalName);
groupMembers[j].SelectSingleNode(xPathExpression);


The resulting xPathExpression is

ns0:DisplayText[1]


The xml looks like this:

<MyXmlTest xmlns="http://www.myxmltest.com/document">
<Ribbon visible="true">
<Group name="xmlProperties">Document Properties</Group>
<Group name="xmlActions">Other Properties</Group>
</Ribbon>
<DocumentList>
<Document>
<Properties ribbonLabel="Hello World Menu">
<Property name="helloWorld">
<RibbonButton groupName="xmlActions">
<DisplayText>Hello World</DisplayText>
<PlaceholderText>N/A</PlaceholderText>
<Tooltip>Some text goes here.</Tooltip>
</RibbonButton>
<Content/>
</Property>
<Property name="title">
<RibbonButton groupName="xmlProperties">
<DisplayText>Hello World Text</DisplayText>
<PlaceholderText>N/A</PlaceholderText>
<Tooltip>Insert Hello World in Document</Tooltip>
</RibbonButton>
<Content>Testing</Content>
</Property>
</Properties>
</Document>
</DocumentList>
</MyXmlTest>

Answer

Usually, to execute XPath containing namespace prefix you need to pass along namespace manager containing the prefix-URI mapping. Since this option isn't available in your case, as a workaround, you can try to ignore namespaces by using local-name() :

*[local-name()='DisplayText'][1]
Comments