Get XML elements with a particular attribute value
I think you could use the null-conditional operator for x.Attribute("attrName").Value and swap arguments of String.Equals to avoid the NullReferenceException as follows:

IEnumerable&lt;XElement&gt; elems =
     xmlDoc.Descendants("elemName")
     .Where(x =&gt;
         filename.Equals(
             x.Attribute("attrName")?.Value,
             StringComparison.OrdinalIgnoreCase
         )
     );