C# 如何以编程方式在XPathExpression实例中使用XPath函数?

C# 如何以编程方式在XPathExpression实例中使用XPath函数?,c#,xml,xpath,namespaces,C#,Xml,Xpath,Namespaces,我当前的程序需要以编程方式创建一个XPathExpression实例以应用于XmlDocument。xpath需要使用一些xpath函数,如“ends with”。但是,我找不到在XPath中使用“ends with”的方法。我 它抛出异常,如下所示 XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8"" ?>

我当前的程序需要以编程方式创建一个XPathExpression实例以应用于XmlDocument。xpath需要使用一些xpath函数,如“ends with”。但是,我找不到在XPath中使用“ends with”的方法。我

它抛出异常,如下所示

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8"" ?>
                        <myXml xmlns=""http://MyNamespace"" xmlns:fn=""http://www.w3.org/2005/xpath-functions""> 
                        <data>Hello World</data>
                    </myXml>");
    XPathNavigator navigator = xdoc.CreateNavigator();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
    nsmgr.AddNamespace("fn", "http://www.w3.org/2005/xpath-functions");

    XPathExpression xpr;
    xpr = XPathExpression.Compile("fn:ends-with(/myXml/data, 'World')", nsmgr);

    object result = navigator.Evaluate(xpr);
    Console.WriteLine(result);
未处理的异常: System.Xml.XPath.XPathException: 命名空间管理器或XsltC上下文 需要。此查询具有前缀, 变量或用户定义的函数。
在 MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree() 在 System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression expr,XPathNodeIt操作员上下文)
在 System.Xml.XPath.XPathNavigator.Evaluate(XPathExpression expr)

代码如下所示:

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8"" ?>
                        <myXml xmlns=""http://MyNamespace"" xmlns:fn=""http://www.w3.org/2005/xpath-functions""> 
                        <data>Hello World</data>
                    </myXml>");
    XPathNavigator navigator = xdoc.CreateNavigator();

    XPathExpression xpr;
    xpr = XPathExpression.Compile("fn:ends-with(/myXml/data, 'World')");

    object result = navigator.Evaluate(xpr);
    Console.WriteLine(result);
XmlDocument xdoc=new XmlDocument();
xdoc.LoadXml(@)
你好,世界
");
XPathNavigator navigator=xdoc.CreateNavigator();
xpathexpressionxpr;
xpr=XPathExpression.Compile(“fn:以(/myXml/data,'World')结尾);
对象结果=navigator.Evaluate(xpr);
控制台写入线(结果);
在编译表达式时,我尝试将代码更改为插入XmlNamespaceManager,如下所示

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8"" ?>
                        <myXml xmlns=""http://MyNamespace"" xmlns:fn=""http://www.w3.org/2005/xpath-functions""> 
                        <data>Hello World</data>
                    </myXml>");
    XPathNavigator navigator = xdoc.CreateNavigator();
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
    nsmgr.AddNamespace("fn", "http://www.w3.org/2005/xpath-functions");

    XPathExpression xpr;
    xpr = XPathExpression.Compile("fn:ends-with(/myXml/data, 'World')", nsmgr);

    object result = navigator.Evaluate(xpr);
    Console.WriteLine(result);
XmlDocument xdoc=new XmlDocument();
xdoc.LoadXml(@)
你好,世界
");
XPathNavigator navigator=xdoc.CreateNavigator();
XmlNamespaceManager nsmgr=新的XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace(“fn”http://www.w3.org/2005/xpath-functions");
xpathexpressionxpr;
xpr=XPathExpression.Compile(“fn:以(/myXml/data,'World')结尾”,nsmgr);
对象结果=navigator.Evaluate(xpr);
控制台写入线(结果);
在XPathExpression上失败。编译调用:

未处理的异常: System.Xml.XPath.XPathException: 此查询需要XsltContext 因为一个未知的函数。在 MS.Internal.Xml.XPath.CompiledXpathExpr.UndefinedXsltContext.resolvefunci on(字符串前缀、字符串名称、, XPathResultType[]ArgTypes)位于 MS.Internal.Xml.XPath.FunctionQuery.SetXsltContext(XsltContext (上下文)在 MS.Internal.Xml.XPath.CompiledXpathExpr.SetContext(XmlNamespaceManager nsM管理者)在 System.Xml.XPath.XPathExpression.Compile(字符串 xpath,IXMLNamespaceResolver nsResolver)

有人知道在XPathExpression.Compile中使用现成XPath函数的诀窍吗?
谢谢

函数不是为定义的,只是为和定义的。

您正在使用.NETNET在此日期未实施,或

可以很容易地构造XPath 1.0表达式,对其求值产生的结果与函数
以()结尾的结果相同:

'World' = 
   substring(/myXml/data,
             string-length(/myXml/data) - string-length('World') +1
             )
$str2=子字符串($str1,字符串长度($str1)-字符串长度($str2)+1)

生成与以下相同的布尔结果(
true()
false()
):

以($str1,$str2)结尾

在您的具体案例中,您只需要用正确的表达式替换
$str1
$str2
。因此,它们是
/myXml/data

因此,要使用的XPath 1.0表达式(相当于XPath 2.0表达式
以(/myXml/data,'World')
结尾)是

'World' = 
   substring(/myXml/data,
             string-length(/myXml/data) - string-length('World') +1
             )