title: Check if any XML nodes exists with Specific Attribute using LINQ C#
body: <p>This is my XML:</p>
  
  <pre><code>&lt;configuration&gt;
      &lt;Script name="Test Script"&gt;
          &lt;arguments&gt;
              &lt;argument key="CheckStats" value="True" /&gt;
              &lt;argument key="ReferenceTimepoint" value="SCREENING" /&gt;
              &lt;argument key="outputResultSetName" value="ResultSet" /&gt;
          &lt;/arguments&gt;
      &lt;/Script&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>I am trying to use this linq statement to grab an <code>argument</code> element's <code>value</code> attrbiute if a specific <code>key</code> attribute exists.</p>
  
  <pre><code>XElement root = XElement.Load(configFileName);
  var AttrVal = from el in root.Elements("Script").Elements("arguments").Elements("argument")
              where el.Attribute("key").Value == "CheckStats"
              select el.Attribute("value").Value;
  </code></pre>
  
  <p>Then I want to try and parse the attribute <code>value</code> into a boolean:</p>
  
  <pre><code>bool checkVal;
  if (AttrVal != null)
  {
      if (!bool.TryParse(AttrVal.First().ToString(), out checkVal))
      {
          throw new Exception(string.Format("Invalid value"));
      }
  }
  </code></pre>
  
  <p>This code works if there is an element with that attribute, but if there isn't one, I get a <code>System.InvalidOperationException: Sequence contains no elements</code>.</p>
  
  <p>How can I get around that?
  I thought by checking <code>if (AttrVal != null)</code> it would work.
  Should I replace that with <code>if (AttrVal.FirstOrDefault() != null)</code> or something similar?
  Thanks</p>