Skip to content

Comments

Modify SqlStreamingXml XmlWriter to internally use a MemoryStream#3974

Open
jimhblythe wants to merge 1 commit intodotnet:mainfrom
jimhblythe:issue-1877
Open

Modify SqlStreamingXml XmlWriter to internally use a MemoryStream#3974
jimhblythe wants to merge 1 commit intodotnet:mainfrom
jimhblythe:issue-1877

Conversation

@jimhblythe
Copy link

Modify SqlStreamingXml XmlWriter to internally use a MemoryStream instead of a StringBuilder.

Note: UTF8Encoding(false) addition in s_writerSettings is consistent with prior default used within StringWriter/StringBuilder

Issues

Fixes #1877 to be O(n)

Testing

Added 2 new Manual tests to ensure linear behavior for single large node, and secondary validation for multiple nodes
image

…tead of a StringBuilder. (dotnet#1877)

Note: UTF8Encoding(false) addition in s_writerSettings is consistent with prior default used within StringWriter/StringBuilder
@jimhblythe jimhblythe requested a review from a team as a code owner February 20, 2026 21:03
@jimhblythe
Copy link
Author

@dotnet-policy-service agree

//_xmlWriter.WriteNode(_xmlReader, true);
// _xmlWriter.Flush();
WriteXmlElement();
// Update memoryStreamRemaining based on the number of chars just written to the MemoryStream
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's written to the MemoryStream is bytes, the number of bytes and chars can be quite different depending on encoding. It might be clearer to just refer to byte counts in the comment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree for a MemoryStream, but within method GetChars() the stream is passed into XmlWriter.Create() with specific Encoding = new UTF8Encoding(false) to prevent the BOM, as well as meet the XML expectation.
The only use for writing is within WriteXmlElement() consistent with only XML characters being written. Changing the comment for this specific method might dilute that only chars are written. Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately, the comment should assist future maintenance so I'm good with modifying it to achieve that goal while also being more inline with MemoryStream semantics.

Something felt unexpected about the prior StringWriter using the default UTF-8 encoding, and why I included "Note: UTF8Encoding(false)" within the PR comment. Perhaps there is a different, existing issue with SqlClient support of other encodings. I've seen references to using FOR XML with non-XML columns causing concerns.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

O(N^2) performance when reading XML with SequentialAccess

2 participants