Office 升级后 Word VSTO CustomXMLNode.SelectSingleNode 失败

Word VSTO CustomXMLNode.SelectSingleNode fails after Office upgrade

单词 vsto 插件在调用 CustomXMLNode.SelectSingleNode 时失败并出现异常。

插件在 word 2007 - 2013 中运行良好,但在 2016 版中它失败并引用未声明的命名空间前缀:'ns0'.

我已经解决了在不使用 xpath 和这些方法的情况下导航 XML 的问题,但仍然需要弄清楚如何解决这个问题。

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

生成的 xPathExpression 是

ns0:DisplayText[1]

xml 看起来像这样:

<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>

通常,要执行包含命名空间前缀的 XPath,您需要 pass along namespace manager 包含前缀-URI 映射。由于此选项在您的情况下不可用,作为解决方法,您可以尝试使用 local-name() 忽略命名空间:

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