抛出异常:需要命名空间管理器或 XsltContext [英] Throw an exception: Namespace Manager or XsltContext needed

查看:35
本文介绍了抛出异常:需要命名空间管理器或 XsltContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个关于使用 XPath 函数的问题 那里

I ask a question about using XPath function there

但是我遇到了一个关于这个异常的问题:

But I meet a problem about this exception:

[System.Xml.XPath.XPathException] = {"Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."}

xml如下:

<x-config>
    <!--user location-->
    <x-list>
      <Country code="TW,UK,MY" />
      <Country code="US,CA,MX" />
      <Country code="IN,PR,VI,IR" />
      <Country code="Others" /> 
    </x-list>

我使用以下代码来检索匹配项:

And I use following code to retrieve matches:

XmlNode countryNode = cdnConfig.SelectSingleNode(
    string.Format("x-config/x-list/Country[fn:contains(@code, {0})]", countryCode)
);

我是 XPath 的新手.有人可以详细说明这一点并提供一些解决方法吗?

I am a newbie of XPath. Could anybody elaborate more on this and give some workaround?

非常感谢.

推荐答案

这个问题应该回答

简而言之:无法识别 XPath 表达式中的 fn 前缀.您可以提供一个 XmlNamespaceManager 实例来声明前缀,但由于 .NET 显然没有 XPath 2.0 支持,因此这无济于事.

In short: The fn prefix in your XPath expression is not recognized. You could supply an instance of XmlNamespaceManager to declare the prefix, but since .NET apparently doesn't have XPath 2.0 support, this won't help.

然而,contains 函数已经被 XPath 1.0 支持,所以不需要 XPath 2.0:

However, the contains function is already supported by XPath 1.0, so no need for XPath 2.0:

XmlNode countryNode = cdnConfig.SelectSingleNode(
    string.Format("x-config/x-list/Country[contains(@code, '{0}')]", countryCode)
);                                         ↑

这篇关于抛出异常:需要命名空间管理器或 XsltContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆