Handle CDATA in unknown extension data (IExtensibleDataObject)#130502
Open
StephenMolloy wants to merge 1 commit into
Open
Handle CDATA in unknown extension data (IExtensibleDataObject)#130502StephenMolloy wants to merge 1 commit into
StephenMolloy wants to merge 1 commit into
Conversation
DataContractSerializer threw a SerializationException when deserializing an unknown element whose value is carried in a CDATA section into ExtensionData (types implementing IExtensibleDataObject). ReadExtensionDataValue's NodeType switch handled Text/Element/EndElement but not CDATA, and MoveToContent stops on CDATA rather than folding it into Text, so the value hit the default case and threw. ReadUnknownXmlData similarly special-cased only Text and would drop leading CDATA content. Treat CDATA like Text in both places. The content is captured as a string node and replays as escaped text on re-serialization, preserving round-trip semantics. Fixes dotnet#59123 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes DataContractSerializer extension-data deserialization for unknown members whose element content is carried in a CDATA section by treating XmlNodeType.CDATA the same as XmlNodeType.Text during extension-data parsing, and adds a regression test to validate down-level round-tripping via IExtensibleDataObject.
Changes:
- Handle
XmlNodeType.CDATAinReadExtensionDataValueso primitive extension data can be read from CDATA content. - Preserve leading CDATA content in
ReadUnknownXmlDataby recognizing CDATA as text content (instead of advancing past it). - Add a regression test and simple versioned contracts to validate ExtensionData capture and round-trip behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/XmlObjectSerializerReadContext.cs | Treat CDATA as text when reading primitive extension data and when capturing unknown XML into XmlDataNode, avoiding exceptions/dropped content. |
| src/libraries/System.Runtime.Serialization.Xml/tests/DataContractSerializer.cs | Adds an xUnit regression test that exercises unknown-member-with-CDATA flowing into ExtensionData and round-tripping back into a newer contract. |
| src/libraries/System.Runtime.Serialization.Xml/tests/SerializationTypes.RuntimeOnly.cs | Adds CdataVersionedV1/V2 contracts implementing IExtensibleDataObject for the new regression test scenario. |
This was referenced Jul 10, 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.
DataContractSerializer threw a SerializationException when deserializing an unknown element whose value is carried in a CDATA section into ExtensionData (types implementing IExtensibleDataObject). ReadExtensionDataValue's NodeType switch handled Text/Element/EndElement but not CDATA, and MoveToContent stops on CDATA rather than folding it into Text, so the value hit the default case and threw. ReadUnknownXmlData similarly special-cased only Text and would drop leading CDATA content.
Treat CDATA like Text in both places. The content is captured as a string node and replays as escaped text on re-serialization, preserving round-trip semantics.
Fixes #59123