charlypu
charlypu

Reputation: 158

How to detect that an XPATH expression is correct?

Is there any way (regex or similar, c# preferred) to detect if an XPath expression is correct before using it?

I have been googling for some time and nothing seems to appear.

Thanks in advance! Carlos.

Upvotes: 0

Views: 114

Answers (1)

dotixx
dotixx

Reputation: 1500

Try it and if there is a thrown XPathException, that mean your XPath is syntactically wrong.

XmlDocument doc = new XmlDocument();
XPathNavigator nav = doc.CreateNavigator();

try 
{
  var res = nav.Compile(xpath);

  // ...
}
catch (XPathException e)
{
  // Handle exception
}

Upvotes: 1

Related Questions