title: Linq to XML: Get all nodes that contain certain children
body: <p>I have the following XML structure:</p>
  
  <pre><code>&lt;Capabilities&gt;
      &lt;Capability ID="1" Name="Capability # 1"&gt;
          &lt;Relations&gt;
              &lt;Relation RelatedTo="2" RelationType="Child"/&gt;
              &lt;Relation RelatedTo="3" RelationType="Child"/&gt;
              &lt;Relation RelatedTo="4" RelationType="Child"/&gt;
              &lt;Relation RelatedTo="5" RelationType="Child"/&gt;
          &lt;/Relations&gt;
      &lt;/Capability&gt;
      &lt;Capability ID="3" Name="Capability # 3"&gt;
          &lt;Relations&gt;
              &lt;Relation RelatedTo="1" RelationType="Dependant" /&gt;
          &lt;/Relations&gt;
      &lt;/Capability&gt;
      &lt;Capability ID="2" Name="Capability # 2"&gt;
          &lt;Relations&gt;
              &lt;Relation RelatedTo="6" RelationType="Child" /&gt;
              &lt;Relation RelatedTo="5" RelationType="Child" /&gt;
          &lt;/Relations&gt;
      &lt;/Capability&gt;
  &lt;/Capabilities&gt;
  </code></pre>
  
  <p>And I need to query the for all Capabilities with a Child relation with RelatedTo attribute of value 5.
  So far I've tried with the following query but I get a collection of 'null reference exceptions' and I can't figure out how to do it.</p>
  
  <pre><code>   var result = root.Elements("Capability")
                   .Where(c =&gt; c.Elements("Relations")
                              .Where(r =&gt; r.Attribute("RelatedTo").Value == "5"
                                  &amp;&amp; r.Attribute("").Value == Enum.GetName(typeof(RelationTypes), RelationTypes.Children)).Any());
  </code></pre>
  
  <p>Any clues on what I'm doing wrong or what's the correct way to query it?</p>