why I get an invalid token while using Xpath if-then-else Expression with c# XPathNavigator Evaluate?

553 views Asked by At

I get an invalid token exception while trying to evaluate below expression via XpathNavigor:

var expression = if(//DovizCins = 'YTL') then '1' else '2';

var nav = doc.CreateNavigator();
XPathExpression xp = XPathExpression.Compile(expression);
var value = nav.Evaluate(xp);

return value?.ToString() ?? string.Empty;

Exception is:

System.Xml.XPath.XPathException: ''if(//DovizCins = 'YTL') then '1' else '2'' has an invalid token.'

2

There are 2 answers

2
Michael Kay On BEST ANSWER

Microsoft's XML technology is way out of date. This is XPath 2.0 syntax, introduced in 2007, and Microsoft has yet to catch up: they're still shipping XPath 1.0.

0
Yitzhak Khabinsky On

I completely concur with Michael Kay.

The official MS documentation is mistaken: https://learn.microsoft.com/en-us/dotnet/api/system.xml.xpath?view=netframework-4.7.1

Excerpt

"...The System.Xml.XPath namespace contains the classes that define a cursor model for navigating and editing XML information items as instances of the XQuery 1.0 and XPath 2.0 Data Model..."

XQuery 1.0 and XPath 2.0 are partially supported by MS SQL Server. The .Net framework doesn't support any XQuery, and its XPath is 1.0