Skip to content

XmlReader.MoveToContent Examples Throw Exception Unless XmlReader Has Correct Conformance Level #9114

Open
@zacharylayne

Description

@zacharylayne

The XmlReader.MoveToContent Method page has example code line: 3465 that can throw the exception System.Xml.XmlException.

This can happen when using a reader that has its conformance level set to document OR reusing a reader initialized with conformance level set to auto.

The example should demonstrate using a reader with Settings.ConformanceLevel set to ConformanceLevel.Fragment. The code will then "handle the following inputs without breaking".

This was tested with the following code:

XmlReader reader = XmlReader.Create("Test.xml", new()
{
    IgnoreProcessingInstructions = true,
    IgnoreWhitespace = true,
    ConformanceLevel = ConformanceLevel.Fragment,
});

byte[] bytes = Encoding.UTF8.GetBytes(
    @"""
    <?xml version=""1.0""><!DOCTYPE price SYSTEM
     ""abc""><price>123.4</price>
    """);

reader = XmlReader.Create(new MemoryStream(bytes));

// Exception thrown here:
if (reader.MoveToContent() == XmlNodeType.Element && reader.Name == "price")
{
    var foo = reader.ReadString();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pri3Indicates issues/PRs that are low priorityarea-System.Xml

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions