How to prevent "Possible NullReferenceException" with LINQ to XML?
I validate my input XML first with a XML Schema file, so I know that several attributes are present and valid. But in the LINQ query, R# always moans about the possible null return values for the Element() or Attribute() calls. I see, in general these methods can return null, but not after I know that my XML is valid according to the XSD.
Any way to prevent these warnings, except switching off the warning completely (which is not what I want), or embedding every single LINQ query inside a "// ReSharper disable PossibleNullReferenceException"?
Please sign in to leave a comment.
Hello Achim,
At the moment there's no other way except removing the external annotations
for those methods under \Bin\ExternalAnnotations.
Thank you!
Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
Just to be sure, you are suggesting removing a part like
<member name="M:System.Xml.Linq.XContainer.Element(System.Xml.Linq.XName)">
<attribute ctor="M:JetBrains.Annotations.CanBeNullAttribute.#ctor" />
</member>
from c:\Program Files\JetBrains\ReSharper\v5.1\Bin\ExternalAnnotations\System.Xml.Linq\System.Xml.Linq.4.0.0.0.Nullness.Generated.xml ?
Hello Achim,
Yes, this will make ReSharper "forget" that XContainer.Element can return
null. Please note that this will affect all solutions on your machine and
that you will need to restart Visual Studio after removing that annotation.
Thank you!
Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"