title: XML - Check for existence of a specific node
body: <p>I don't know why I'm having so much trouble with this, but I'm hoping someone can get me pointed in the right direction.</p>
  
  <p>I have these few lines of code :</p>
  
  <pre><code>var xDoc = new XmlDocument();
  xDoc.LoadXml(xelementVar.ToString());
  
  if (xDoc.ChildNodes[0].HasChildNodes)
  {
      for (int i = 0; i &lt; xDoc.ChildNodes[0].ChildNodes.Count; i++)
      {
          var sFormatId = xDoc.ChildNodes[0].ChildNodes[i].Attributes["formatID"].Value;
          // Do some stuff
      }    
  // Do some more stuff
  }
  </code></pre>
  
  <p>The problem is that the <code>xDoc</code> I'm getting doesn't always have the <code>formatID</code> node, so I end up getting a null reference exception, although 99% of the time it works perfectly fine.</p>
  
  <p><strong>My question :</strong>  </p>
  
  <p>How can I check if the <code>formatID</code> node exists before I try to read the <code>Value</code> out of it?  </p>