tags:

views:

32

answers:

1

I'm looking for a quick and easy method to check if a given string is a valid xpath. Does anybody know if something like this exists?

Thanks,

helixed

+2  A: 

You could try to compile it into a XPathExpression. http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathexpression.aspx

The nice side-benefit is that, if it works, you have a compiled expression that you can just execute.

Steven Sudit
Hey, this worked. I used XPathExpression.Compile() and I caught the exceptions. I wish there was a less expensive way to do this though. Thanks.
helixed
Think of it in terms of amortization: once you pay the expense of compiling an expression to determine whether it's valid, you now have an expression you can efficiently use over and over. The alternative would be to just call XPathNavigator.Select(string) on an empty XPathDocument and catching exceptions.
Steven Sudit