title: Best way test for XPath existence in an XML file?
body: <p>Lately I've been using XPathDocument and XNavigator to parse an XML file for a given XPath and attribute. It's been working very well, when I know in advance what the XPath is. </p>
  
  <p>Sometimes though, the XPath will be one of several possible XPath values, and I'd like to be able to test whether or not a given XPath exists. </p>
  
  <p>In case I'm getting the nomenclature wrong, here's what I'm calling an XPath - given this XML blob:</p>
  
  <pre><code>&lt;foo&gt;
      &lt;bar baz="This is the value of the attribute named baz"&gt;
  &lt;/foo&gt;
  </code></pre>
  
  <p>I might be looking for what I'm calling an XPath of "//foo/bar" and then reading the attribute "baz" to get the value. </p>
  
  <p>Example of the code that I use to do this: </p>
  
  <pre><code>XPathDocument document = new XPathDocument(filename);
  XPathNavigator navigator = document.CreateNavigator();
  XPathNavigator node = navigator.SelectSingleNode("//foo/bar");
  if(node.HasAttributes)
  {
      Console.WriteLine(node.GetAttribute("baz", string.Empty));
  }
  </code></pre>
  
  <p>Now, if the call to navigator.SelectSingleNode fails, it will return a NullReferenceException or an XPathException. I can catch both of those and refactor the above into a test to see whether or not a given XPath returns an exception, but I was wondering whether there was a better way? </p>
  
  <p>I didn't see anything obvious in the Intellisense. XPathNavigator has .HasAttributes and .HasChildren but short of iterating through the path one node at a time, I don't see anything nicer to use. </p>