XPath 扩展方法在 .NET 3.5 中失败,但在 .NET 4.0 中有效

发布于 2024-11-17 19:40:45 字数 2286 浏览 1 评论 0原文

我创建了一个标准的 XsltContext 类并按如下方式调用它:

        XPathCustomContext context = new XPathCustomContext(new NameTable());
        context.AddNamespace("windward", XPathCustomContext.Namespace);
        XsltArgumentList varList = new XsltArgumentList();
        varList.AddParam("stat-index", "", 0);
        context.ArgList = varList;

        XmlDocument doc = new XmlDocument();
        doc.Load("c:\\test\\order.xml");
        object xx = doc.CreateNavigator().Evaluate("/order[1]/customer[1]/num[@negone = $stat-index]", context);

在 .net 4.0 下运行时它工作正常。但在 .NET 3.5(我们目前必须使用它)下,我得到:

System.Xml.XPath.XPathException was unhandled
  Message=XsltContext is needed for this query because of an unknown function.
  Source=System.Xml
  StackTrace:
       at MS.Internal.Xml.XPath.CompiledXpathExpr.UndefinedXsltContext.ResolveVariable(String prefix, String name)
       at MS.Internal.Xml.XPath.VariableQuery.SetXsltContext(XsltContext context)
       at MS.Internal.Xml.XPath.LogicalExpr.SetXsltContext(XsltContext context)
       at MS.Internal.Xml.XPath.FilterQuery.SetXsltContext(XsltContext input)
       at MS.Internal.Xml.XPath.CompiledXpathExpr.SetContext(XmlNamespaceManager nsManager)
       at System.Xml.XPath.XPathExpression.Compile(String xpath, IXmlNamespaceResolver nsResolver)
       at System.Xml.XPath.XPathNavigator.Evaluate(String xpath, IXmlNamespaceResolver resolver)
       at CustomXpathFunctions.Program.Main(String[] args) in c:\src\CustomXpathFunctions\Program.cs:line 62
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

知道为什么吗?

示例代码位于 http://www.windwardreports.com/temp/CustomXPathFunctionsBug.zip

谢谢- 戴夫

I created a standard XsltContext class and call it as follows:

        XPathCustomContext context = new XPathCustomContext(new NameTable());
        context.AddNamespace("windward", XPathCustomContext.Namespace);
        XsltArgumentList varList = new XsltArgumentList();
        varList.AddParam("stat-index", "", 0);
        context.ArgList = varList;

        XmlDocument doc = new XmlDocument();
        doc.Load("c:\\test\\order.xml");
        object xx = doc.CreateNavigator().Evaluate("/order[1]/customer[1]/num[@negone = $stat-index]", context);

When running under .net 4.0 it works fine. But under .NET 3.5 (which we have to use at present) I get:

System.Xml.XPath.XPathException was unhandled
  Message=XsltContext is needed for this query because of an unknown function.
  Source=System.Xml
  StackTrace:
       at MS.Internal.Xml.XPath.CompiledXpathExpr.UndefinedXsltContext.ResolveVariable(String prefix, String name)
       at MS.Internal.Xml.XPath.VariableQuery.SetXsltContext(XsltContext context)
       at MS.Internal.Xml.XPath.LogicalExpr.SetXsltContext(XsltContext context)
       at MS.Internal.Xml.XPath.FilterQuery.SetXsltContext(XsltContext input)
       at MS.Internal.Xml.XPath.CompiledXpathExpr.SetContext(XmlNamespaceManager nsManager)
       at System.Xml.XPath.XPathExpression.Compile(String xpath, IXmlNamespaceResolver nsResolver)
       at System.Xml.XPath.XPathNavigator.Evaluate(String xpath, IXmlNamespaceResolver resolver)
       at CustomXpathFunctions.Program.Main(String[] args) in c:\src\CustomXpathFunctions\Program.cs:line 62
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Any idea why?

Sample code at http://www.windwardreports.com/temp/CustomXPathFunctionsBug.zip

thanks - dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零時差 2024-11-24 19:40:45

想出了一个方法来做到这一点。使用如下 XPathExpression:

XPathNavigator nav = doc.CreateNavigator();
XPathExpression exp = nav.Compile("/order[1]/customer[1]/num[@negone = $stat]");
exp.SetContext(ctx);
object zzz = nav.Evaluate(exp);

至于为什么 - 我的猜测是 Evaluate(string, context) 仅在 .NET 3.5 中将其用于命名空间,而 XPathExpression 将其用于所有内容。但关键点是这有效。

ps - 请对这个答案的鼻涕投赞成票 - 这真的很难弄清楚。

Figured out a way to do this. Use an XPathExpression like:

XPathNavigator nav = doc.CreateNavigator();
XPathExpression exp = nav.Compile("/order[1]/customer[1]/num[@negone = $stat]");
exp.SetContext(ctx);
object zzz = nav.Evaluate(exp);

As to why - my guess is Evaluate(string, context) uses it for namespaces only in .NET 3.5 while XPathExpression uses it for everything. The critical point though is this works.

ps - Please upvote the snot out of this answer - this was really hard to figure out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文