title: A better solution than element.Elements("Whatever").First()?
body: <p>I have an XML file like this:</p>
  
  <pre><code>&lt;SiteConfig&gt;
    &lt;Sites&gt;
      &lt;Site Identifier="a" /&gt;
      &lt;Site Identifier="b" /&gt;
      &lt;Site Identifier="c" /&gt;
    &lt;/Sites&gt;
  &lt;/SiteConfig&gt;
  </code></pre>
  
  <p>The file is user-editable, so I want to provide reasonable error message in case I can't properly parse it. I could probably write a .xsd for it, but that seems kind of overkill for a simple file.</p>
  
  <p>So anyway, when querying for the list of <code>&lt;Site&gt;</code> nodes, there's a couple of ways I could do it:</p>
  
  <pre><code>var doc = XDocument.Load(...);
  
  var siteNodes = from siteNode in 
                    doc.Element("SiteConfig").Element("Sites").Elements("Site")
                  select siteNode;
  </code></pre>
  
  <p>But the problem with this is that if the user has not included the <code>&lt;SiteUrls&gt;</code> node (say) it'll just throw a <code>NullReferenceException</code> which doesn't really say much to the user about what actually went wrong.</p>
  
  <p>Another possibility is just to use <code>Elements()</code> everywhere instead of <code>Element()</code>, but that doesn't always work out when coupled with calls to <code>Attribute()</code>, for example, in the following situation:</p>
  
  <pre><code>var siteNodes = from siteNode in 
                    doc.Elements("SiteConfig")
                       .Elements("Sites")
                       .Elements("Site")
                  where siteNode.Attribute("Identifier").Value == "a"
                  select siteNode;
  </code></pre>
  
  <p>(That is, there's no equivalent to <code>Attributes("xxx").Value</code>)</p>
  
  <p>Is there something built-in to the framework to handle this situation a little better? What I would prefer is a version of <code>Element()</code> (and of <code>Attribute()</code> while we're at it) that throws a descriptive exception (e.g. "Looking for element &lt;xyz&gt; under &lt;abc&gt; but no such element was found") instead of returning <code>null</code>.</p>
  
  <p>I could write my own version of <code>Element()</code> and <code>Attribute()</code> but it just seems to me like this is such a common scenario that I must be missing something...</p>