私の現在のプログラムでは、XmlDocument に適用する XPathExpression インスタンスをプログラムで作成する必要があります。xpath では、「ends-with」などの XPath 関数を使用する必要があります。しかし、XPath で「ends-with」を使用する方法が見つかりません。私
以下のような例外をスローします
未処理の例外: System.Xml.XPath.XPathException: Namespace Manager または XsltC ontext が必要です。このクエリには、プレフィックス、変数、またはユーザー定義関数があります。
MS.Internal.Xml.XPath.CompiledXpathExpr.get_QueryTree() で System.Xml.XPath.XPathNavigator.Evaluate (XPathExpression expr、XPathNodeIterator コンテキスト)
で 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);
以下のように、式をコンパイルするときに 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);
XPathExpression.Compile 呼び出しで失敗します。
未処理の例外: System.Xml.XPath.XPathException: 不明な関数のため、このクエリには XsltContext が必要です。MS.Internal.Xml.XPath.CompiledXpathExpr.UndefinedXsltContext.ResolveFuncti on(String prefix, String name, XPathResultType[] ArgTypes) で MS.Internal.Xml.XPath.FunctionQuery.SetXsltContext(XsltContext コンテキスト) で MS.Internal.Xml. System.Xml.XPath.XPathExpression.Compile(String xpath, IXmlNamespaceResolver nsResolver) の XPath.CompiledXpathExpr.SetContext(XmlNamespaceManager nsM anager)
XPathExpression.Compile で既製の XPath 関数を使用するコツを知っている人はいますか? ありがとう