You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.Xml.XmlNode.SelectNodes return should not be nullable #49131
changed the title [-]System.Xml.XmlDOcument.SelectNodes return should not be nullable[/-][+]System.Xml.XmlNode.SelectNodes return should not be nullable[/+]on Mar 4, 2021
There are cases where the result can be null, see comments in the source.
OK I see, I would expect those cases to throw instead of returning null. I understand it would be a breaking change to start throwing instead of returning null at this point, but perhaps it makes sense? I imagine this is a very rare situation, and it's easy enough to add a static flag property which reverts back to returning null for backwards compatibility. The benefit would be that signature is non-nullable return which is appropriate in 99.99% of the cases.
@MrTA2 yes and no, I can imagine people calling this recursively which would be super annoying to have to handle each type separately. Perhaps the right answer might be to make this nullable when called on XNode but if you call it directly on XmlElement or so you'd get non-null, i.e.:
partialclassXmlElement{// perhaps aggressive inlining so this is no-oppublicnewXmlNodeListSelectNodes(stringxpath){returnbase.SelectNodes(xpath)!;}}
I'm not sure if API review will be in favor of that. Would that be helpful assuming they agreed?
@MrTA2 yes and no, I can imagine people calling this recursively which would be super annoying to have to handle each type separately. Perhaps the right answer might be to make this nullable when called on XNode but if you call it directly on XmlElement or so you'd get non-null, i.e.:
I'm not sure if API review will be in favor of that. Would that be helpful assuming they agreed?
Yes this makes sense, and please also add XmlDocument.SelectNodes because it inherits directly from XmlNode. Thank you!!
Fail: I just tried adding extension methods on XmlElement and XmlDocument, but overload resolution prefers instance methods of the base class instead of extension method of the child class.
Honestly looking at this issue again, I don't believe it's worth to add these APIs just for the sake of nullability. Instead I'd recommend extension method:
staticclassXmlNodeExtensions{// use whatever name suits youpublicstaticXmlNodeListSelectNodes2(thisXmlNodenode,stringxpath,XmlNamespaceManager?nsmgr=null){if(nsmgr==null){returnnode.SelectNodes(xpath)??newEmptyNodeList();}else{returnnode.SelectNodes(xpath,nsmgr)??newEmptyNodeList();}}privateclassEmptyNodeList:XmlNodeList{publicoverrideintCount=>0;publicoverrideIEnumeratorGetEnumerator()=>Array.Empty<XmlNode>().GetEnumerator();publicoverrideXmlNode?Item(intindex)=>null;}}
I hope you reconsider. This is becoming more and more of an issue where across multiple types and verticals in BCL, things are marked as returning nullable where it doesn't make much sense. This seriously hinders the usefulness of NRT as a feature. The purpose of it is, allow developers to clearly indicate what can and cannot be null; these nullable return methods blur that line.
Yes I can create such extension methods, in fact, I have already created a few methods like that, but I don't quite understand why BCL has to have all these quirks with nullability.
ghost locked as resolved and limited conversation to collaborators on Sep 1, 2022
Activity
[-]System.Xml.XmlDOcument.SelectNodes return should not be nullable[/-][+]System.Xml.XmlNode.SelectNodes return should not be nullable[/+]Tornhoof commentedon Mar 4, 2021
You might want to take a look at the source code:
runtime/src/libraries/System.Private.Xml/src/System/Xml/Dom/XmlNode.cs
Lines 71 to 94 in 79ae74f
There are cases where the result can be null, see comments in the source.
MrTA2 commentedon Mar 4, 2021
OK I see, I would expect those cases to throw instead of returning null. I understand it would be a breaking change to start throwing instead of returning null at this point, but perhaps it makes sense? I imagine this is a very rare situation, and it's easy enough to add a static flag property which reverts back to returning null for backwards compatibility. The benefit would be that signature is non-nullable return which is appropriate in 99.99% of the cases.
krwq commentedon Mar 4, 2021
@MrTA2 yes and no, I can imagine people calling this recursively which would be super annoying to have to handle each type separately. Perhaps the right answer might be to make this nullable when called on XNode but if you call it directly on XmlElement or so you'd get non-null, i.e.:
I'm not sure if API review will be in favor of that. Would that be helpful assuming they agreed?
MrTA2 commentedon Mar 4, 2021
Yes this makes sense, and please also add
XmlDocument.SelectNodesbecause it inherits directly fromXmlNode. Thank you!!MrTA2 commentedon Mar 4, 2021
Fail: I just tried adding extension methods on
XmlElementandXmlDocument, but overload resolution prefers instance methods of the base class instead of extension method of the child class.krwq commentedon Mar 4, 2021
that would have to be added directly in the framework. I meant to add it in all places where it's not null, XmlElement was just an example
krwq commentedon Aug 2, 2022
Honestly looking at this issue again, I don't believe it's worth to add these APIs just for the sake of nullability. Instead I'd recommend extension method:
TahirAhmadov commentedon Aug 2, 2022
I hope you reconsider. This is becoming more and more of an issue where across multiple types and verticals in BCL, things are marked as returning nullable where it doesn't make much sense. This seriously hinders the usefulness of NRT as a feature. The purpose of it is, allow developers to clearly indicate what can and cannot be null; these nullable return methods blur that line.
Yes I can create such extension methods, in fact, I have already created a few methods like that, but I don't quite understand why BCL has to have all these quirks with nullability.