Skip to content

Implement support for XmlAttribute in WriteAttribute method - #129489

Merged
StephenMolloy merged 6 commits into
dotnet:mainfrom
StephenMolloy:1398-xmlserializer-prereq
Jul 9, 2026
Merged

StephenMolloy merged 6 commits into
dotnet:mainfrom
StephenMolloy:1398-xmlserializer-prereq

Conversation

@StephenMolloy

@StephenMolloy StephenMolloy commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

This pull request addresses support for serializing XML attributes as nodes when TypeDesc.CanBeAttributeValue is true, and moves the corresponding test to the main test suite. The changes ensure correct handling of XML attributes in the reflection-based serializer and improve test coverage.

Serializer logic improvements:

  • Updated WriteAttribute in ReflectionXmlSerializationReader.cs to handle cases where TypeDesc.CanBeAttributeValue is true by checking if the attribute is an XmlAttribute and returning early if not, instead of throwing a NotImplementedException. This resolves a previously unimplemented scenario.

Test suite updates:

  • Moved the Xml_CustomDocumentWithXmlAttributesAsNodes test from XmlSerializerTests.Internal.cs to the main XmlSerializerTests.cs file, reflecting that the serializer now supports this scenario. [1] [2]

Fixes #1398

@StephenMolloy StephenMolloy added this to the 11.0.0 milestone Jun 16, 2026
@StephenMolloy
StephenMolloy requested a review from mangod9 June 16, 2026 23:41
@StephenMolloy StephenMolloy self-assigned this Jun 16, 2026
Copilot AI review requested due to automatic review settings June 16, 2026 23:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the reflection-based XmlSerializer reader to handle attributes whose mapping type can accept an attribute node (via TypeDesc.CanBeAttributeValue), and promotes the related regression test into the main XmlSerializerTests suite.

Changes:

  • Updated ReflectionXmlSerializationReader.WriteAttribute to accept XmlAttribute instances for SpecialMapping types where TypeDesc.CanBeAttributeValue is true, instead of throwing NotImplementedException.
  • Moved Xml_CustomDocumentWithXmlAttributesAsNodes from XmlSerializerTests.Internal.cs into XmlSerializerTests.cs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs Implements the missing CanBeAttributeValue attribute-node handling by consuming XmlAttribute values when provided.
src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.Internal.cs Removes the previously “internal-only” placement of the regression test.
src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs Adds the promoted test to the main test suite.

assert round-trip

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings June 30, 2026 00:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

quote fix on test

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings June 30, 2026 00:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

missing brace

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI review requested due to automatic review settings June 30, 2026 01:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Add null check for XmlAttribute and handle CheckSpecified.
@StephenMolloy
StephenMolloy merged commit e302d2a into dotnet:main Jul 9, 2026
88 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-preview7 Jul 10, 2026
eiriktsarpalis pushed a commit that referenced this pull request Jul 15, 2026
This pull request addresses support for serializing XML attributes as
nodes when `TypeDesc.CanBeAttributeValue` is true, and moves the
corresponding test to the main test suite. The changes ensure correct
handling of XML attributes in the reflection-based serializer and
improve test coverage.

**Serializer logic improvements:**

* Updated `WriteAttribute` in `ReflectionXmlSerializationReader.cs` to
handle cases where `TypeDesc.CanBeAttributeValue` is true by checking if
the attribute is an `XmlAttribute` and returning early if not, instead
of throwing a `NotImplementedException`. This resolves a previously
unimplemented scenario.

**Test suite updates:**

* Moved the `Xml_CustomDocumentWithXmlAttributesAsNodes` test from
`XmlSerializerTests.Internal.cs` to the main `XmlSerializerTests.cs`
file, reflecting that the serializer now supports this scenario.
[[1]](diffhunk://#diff-330a912d3ead6a189b066ec6f77af596016258af0e5541ae97e11cfc517a4a32L14-L34)
[[2]](diffhunk://#diff-cb25d8cca02e9c7959514c29f15823cfbdcae1b51cd148ddc69e2a8a6fb780e4R628-R647)

Fixes #1398

---------

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ReflectionBasedXmlSerializer] To Support special.TypeDesc.CanBeAttributeValue == true

3 participants