Reputation: 158
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
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