Implement XmlSerializer end element handling workaround and tests#126765
Merged
StephenMolloy merged 2 commits intodotnet:mainfrom Apr 30, 2026
Merged
Implement XmlSerializer end element handling workaround and tests#126765StephenMolloy merged 2 commits intodotnet:mainfrom
StephenMolloy merged 2 commits intodotnet:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves XmlSerializer behavior when deserializing XML fragments over streaming transports by avoiding an extra read after completing a top-level end element (configurable via an AppContext switch), and adds tests to validate fragment deserialization behavior with the workaround enabled/disabled.
Changes:
- Added
UseXmlSerializerReadEndElementWorkaroundAppContext switch (defaulttrue) to control end-element read behavior. - Updated
XmlSerializationReader.ReadEndElement()andXmlSerializerentry points to avoid unnecessary reads after a top-level end element while still supporting multi-root fragment workflows. - Added fragment/streaming-focused tests plus a custom stream and improved AppContext-switch test scoping.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/System.Private.Xml/src/System/Xml/Core/LocalAppContextSwitches.cs | Introduces the new AppContext switch (default enabled). |
| src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializationReader.cs | Implements the end-element handling workaround in ReadEndElement(). |
| src/libraries/System.Private.Xml/src/System/Xml/Serialization/XmlSerializer.cs | Advances past top-level end elements when starting Deserialize / CanDeserialize to enable multi-fragment reading. |
| src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs | Adds streaming/fragment tests and supporting test infrastructure (custom stream + switch scope improvements). |
This was referenced Apr 11, 2026
mangod9
approved these changes
Apr 23, 2026
This was referenced Apr 28, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request improves handling of XML fragments over streaming transports, particularly when deserializing multiple root elements. The changes add a workaround (enabled by default) to avoid unnecessary reads after a top-level end element, preventing timeouts or exceptions when more data is not immediately available. An AppContext opt-out is also included. The PR also adds comprehensive tests and supporting infrastructure to validate and control this behavior.
XML Serializer Compatibility and Behavior Improvements:
UseXmlSerializerReadEndElementWorkaroundswitch (default: true) toLocalAppContextSwitchesto control workaround behavior for reading end elements in XML fragments.XmlSerializationReader.ReadEndElement()to respect the new switch, avoiding extra reads after a top-level end element when enabled, which prevents blocking or timeouts in streaming/fragment scenarios.AdvancePastTopLevelEndElementIfNeeded(XmlReader)utility and invoked it inXmlSerializermethods (GetMapping,CanDeserialize) to ensure correct reader advancement when the workaround is enabled. [1] [2] [3]Testing and Infrastructure Enhancements:
XmlSerializerTests.csto verify deserialization of XML fragments with and without the workaround, including a test that disables the switch and expects a timeout exception.BlockingAfterBufferStream, a customStreamthat simulates blocking behavior after buffer exhaustion, to facilitate robust testing of streaming scenarios.XmlSerializerAppContextSwitchScopeto properly unset AppContext switches and clear cached values, ensuring test isolation and reliability.Fixes: #47371