0

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.

4

1 回答 1

1

尝试一下,如果抛出 XPathException,则意味着您的 XPath 在语法上是错误的。

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

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

  // ...
}
catch (XPathException e)
{
  // Handle exception
}
于 2013-07-17T13:04:11.700 回答