Skip to content

System.Xml.XmlNode.SelectNodes return should not be nullable #49131

Closed

Description

@MrTA2

Description

No need for it to be nullable if it returns empty collection when no nodes found:

var xd = new XmlDocument();
Console.WriteLine(xd.SelectNodes("//test").Count); // output: 0 (not NullReferenceException)

Configuration

.NET 5, any OS/architecture

Regression?

N/A

Other information

Activity

  1. ghost added
    untriaged
    on Mar 4, 2021
  2. 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
  3. Tornhoof commented on Mar 4, 2021

    @Tornhoof
    Contributor

    You might want to take a look at the source code:

    public XmlNodeList? SelectNodes(string xpath)
    {
    XPathNavigator? n = (this).CreateNavigator();
    //if the method is called on node types like DocType, Entity, XmlDeclaration,
    //the navigator returned is null. So just return null from here for those node types.
    if (n == null)
    return null;
    return new XPathNodeList(n.Select(xpath));
    }
    // Selects all nodes that match the xpath expression and given namespace context.
    public XmlNodeList? SelectNodes(string xpath, XmlNamespaceManager nsmgr)
    {
    XPathNavigator? xn = (this).CreateNavigator();
    //if the method is called on node types like DocType, Entity, XmlDeclaration,
    //the navigator returned is null. So just return null from here for those node types.
    if (xn == null)
    return null;
    XPathExpression exp = xn.Compile(xpath);
    exp.SetContext(nsmgr);
    return new XPathNodeList(xn.Select(exp));
    }

    There are cases where the result can be null, see comments in the source.

  4. MrTA2 commented on Mar 4, 2021

    @MrTA2
    Author

    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.

  5. krwq commented on Mar 4, 2021

    @krwq
    Member

    @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.:

    partial class XmlElement
    {
      // perhaps aggressive inlining so this is no-op
      public new XmlNodeList SelectNodes(string xpath)
      {
        return base.SelectNodes(xpath)!;
      }
    }

    I'm not sure if API review will be in favor of that. Would that be helpful assuming they agreed?

  6. MrTA2 commented on Mar 4, 2021

    @MrTA2
    Author

    @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!!

  7. MrTA2 commented on Mar 4, 2021

    @MrTA2
    Author

    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.

  8. krwq commented on Mar 4, 2021

    @krwq
    Member

    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

  9. added this to the 7.0.0 milestone on Jul 12, 2021
  10. removed
    untriaged
    on Jul 12, 2021
  11. krwq commented on Aug 2, 2022

    @krwq
    Member

    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:

    static class XmlNodeExtensions
    {
        // use whatever name suits you
        public static XmlNodeList SelectNodes2(this XmlNode node, string xpath, XmlNamespaceManager? nsmgr = null)
        {
            if (nsmgr == null)
            {
                return node.SelectNodes(xpath) ?? new EmptyNodeList();
            }
            else
            {
                return node.SelectNodes(xpath, nsmgr) ?? new EmptyNodeList();
            }
        }
    
        private class EmptyNodeList : XmlNodeList
        {
            public override int Count => 0;
    
            public override IEnumerator GetEnumerator() => Array.Empty<XmlNode>().GetEnumerator();
    
            public override XmlNode? Item(int index) => null;
        }
    }
  12. Repository owner moved this from Future to Done in Triage POD for Reflection, META, etc.on Aug 2, 2022
  13. TahirAhmadov commented on Aug 2, 2022

    @TahirAhmadov

    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.

  14. ghost locked as resolved and limited conversation to collaborators on Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions