title: Remove element from XML based on attribute value?
body: <p>I was trying to remove a descendant element from an XElement <em>(using .Remove())</em> and I seem to get a null object reference, and I'm not sure why.</p>
  
  <p>Having looked at the previous question with this title <em>(<a href="https://stackoverflow.com/questions/5004481/">see here</a>)</em>, I found a way to remove it, but I still don't see why the way I tried 1st didn't work.</p>
  
  <p>Can someone enlighten me ?</p>
  
  <pre><code>  String xml = "&lt;things&gt;"
               + "&lt;type t='a'&gt;"
               + "&lt;thing id='100'/&gt;"
               + "&lt;thing id='200'/&gt;"
               + "&lt;thing id='300'/&gt;"
               + "&lt;/type&gt;"
               + "&lt;/things&gt;";
  
    XElement bob = XElement.Parse(xml);
  
    // this doesn't work...
    var qry = from element in bob.Descendants()
              where element.Attribute("id").Value == "200"
              select element;
    if (qry.Count() &gt; 0)
       qry.First().Remove();
  
    // ...but this does
    bob.XPathSelectElement("//thing[@id = '200']").Remove();
  </code></pre>
  
  <p>Thanks,
  Ross</p>