Skip to content

XmlReader.GetAttribute implementation does not match documentation #123749

@drieseng

Description

@drieseng

Description

The following overloads of XmlReader.GetAttribute(...) are documented as having "The value of the specified attribute. If the attribute is not found or the value is String.Empty, null is returned." as return value:

  • GetAttribute(string name, string? namespaceURI);
  • GetAttribute(string name)

However, both overloads return an empty string when the value of the attribute is an empty string.

Reproduction Steps

Compile and run the following code:

using System.IO;
using System.Xml;

internal static class Program
{
    private static void Main()
    {
        string xml = """<Foo name="" />""";

        using (var sr = new StringReader(xml))
        using (var xmlReader = XmlReader.Create(sr))
        {
            xmlReader.MoveToContent();

            var name = xmlReader.GetAttribute("name");
            System.Console.WriteLine("#1 Is null? " + (name is null));
            System.Console.WriteLine("#1 Is empty? " + (name is { Length: 0 }));

            name = xmlReader.GetAttribute("name", null);
            System.Console.WriteLine("#2 Is null? " + (name is null));
            System.Console.WriteLine("#2 Is empty? " + (name is { Length: 0 }));

            name = xmlReader.GetAttribute(0);
            System.Console.WriteLine("#3 Is null? " + (name is null));
            System.Console.WriteLine("#3 Is empty? " + (name is { Length: 0 }));
        }
    }
}

Expected behavior

According to the documentation, the result ought to be:

#1 Is null? True
#1 Is empty? False
#2 Is null? True
#2 Is empty? False
#3 Is null? True
#3 Is empty? False

Actual behavior

#1 Is null? False
#1 Is empty? True
#2 Is null? False
#2 Is empty? True
#3 Is null? False
#3 Is empty? True

Regression?

I consider this to be a documentation issue.
For compatibility, the implementation should not be changed.

Known Workarounds

No response

Configuration

.NET v10.0.101 x64

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.XmluntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions