7

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.

Example XPath: "/MyDocument/MyNode[matches(@MyAttribute, 'MyValue', 'i')]"

I have tried using:

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

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?

Thanks

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
AAADad
  • 153
  • 2
  • 9

1 Answers1

8

.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?

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:

  • 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
Community
  • 1
  • 1
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
  • Is it still actual? I found conflicting information. – SerG Sep 26 '14 at 15:30
  • @SerG my response is a few years old and I didn't find anything new on this subject. If you have new information that this is now supported please post it as an answer. – Ahmad Mageed Sep 26 '14 at 16:21
  • See also the [UserVoice suggestion](https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/3795831-native-support-for-xpath-2-0-or-xslt-2-0-in-net) on this topic. – CodeFox Feb 01 '16 at 09:43