如何使用XPath 2.0方法在.NET 4.0中? [英] How to use XPath 2.0 Methods in .NET 4.0?

查看:191
本文介绍了如何使用XPath 2.0方法在.NET 4.0中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的.NET 4.0,我想使用XPath 2.0的方法,如([匹配()] [1],[大写()] [2],[小写()] [3 ])试图找到文档中的元素时。

I am using .NET 4.0 and I would like to use XPath 2.0 methods such as ([Matches()][1], [upper-case()][2], [lower-case()][3]) when trying to find elements in a document.

例的XPath:/ myDocument中/ MYNODE [匹配(@MyAttribute,'myvalue的','我')]

我已经尝试使用:

  • System.Xml.XPath.XPathNavigator.Compile()
  • System.Xml.XmlDocument.SelectNodes()
  • System.Xml.Linq.XDocument.SelectElements()
  • System.Xml.XPath.XPathNavigator.Compile()
  • System.Xml.XmlDocument.SelectNodes()
  • System.Xml.Linq.XDocument.SelectElements()

不过,我基本上抛出异常UndefinedXsltContextException(或类似的东西)。可以在.NET 4.0中这样做,如果是你能提供关于如何设置它的工作?一个小例子

But I basically throw the exception "UndefinedXsltContextException" (or something similar). Can this be done in .NET 4.0 and if so can you provide a small example on how to set it up to work?

感谢

推荐答案

.NET当前不支持的XPath 2.0。看到这一问题的详细信息和第三方替代方案: XPath和XSLT 2.0 .NET

.NET doesn't currently support XPath 2.0. See this question for more details and third-party alternatives: XPath and XSLT 2.0 for .NET?

如果你不希望使用第三方库,你可以做的最低要求查询,让您的目标元素(S)与任何的XPath 1.0的LINQ to XML,然后做使用.NET方法将数据的额外工作进行检查和修改所需的:

If you don't want to use third-party libraries you could do the minimum required query to get your target element(s) with either XPath 1.0 or LINQ to XML, then do additional work on the data using .NET methods to perform the checks and modifications desired:

  • 匹配 = Regex.IsMatch - 要知道,普通的XPath前pression模式可能有不同的元字符不是.NET的模式,所以有些翻译可能需要
  • 大写 = String.ToUpper - 链接提到文化/不变的选择也一样,如果你需要它们
  • 小写 = String.ToLower
  • Matches = Regex.IsMatch - be aware that the XPath regular expression pattern might have different metacharacters than the .NET pattern, so some translation might be needed.
  • upper-case = String.ToUpper - the link mentions culture/invariant options too, in case you need them
  • lower-case = String.ToLower

这篇关于如何使用XPath 2.0方法在.NET 4.0中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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