Skip to content

Exception thrown when streaming certain XML values via XmlReader #4756

Description

@sharpjs

Describe the bug

When streaming with CommandBehavior.SequentialAccess, if an xml value is a fragment beginning with any character other than <, then SqlDataReader.GetXmlReader(…) returns a reader doomed to throw an exception.

System.Xml.XmlException
  HResult=0x80131940
  Message='.', hexadecimal value 0x00, is an invalid character. Line 1, position 2.
  Source=System.Private.Xml
  StackTrace:
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
   at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars)
   at System.Xml.XmlTextReaderImpl.ParseText()
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at Program.<Main>$(String[] args) in D:\Code\Self\XmlReaderEncodingBug\XmlReaderEncodingBug\Program.cs:line 14

This issue is fixed by #4757.

To reproduce

using Microsoft.Data.SqlClient;
using System.Data;

using var connection = new SqlConnection("Server=.;Integrated Security=true;Encrypt=false");
connection.Open();

using var command = connection.CreateCommand();
command.CommandText = "SELECT CONVERT(xml, 'a<b/>');";

using var results = command.ExecuteReader(CommandBehavior.SequentialAccess);
results.Read(); // => true

using var xml = results.GetXmlReader(0);
xml.Read(); // throws

Expected behavior

Program exits cleanly.

Further technical details

Microsoft.Data.SqlClient version: 7.1.0
.NET target: .NET 10.0
SQL Server version: SQL Server 2025
Operating system: Windows 11 25H2

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions