Skip to content

Implement support for XmlAttribute in WriteAttribute method #129489

Open
StephenMolloy wants to merge 1 commit into
dotnet:mainfrom
StephenMolloy:1398-xmlserializer-prereq
Open

Implement support for XmlAttribute in WriteAttribute method #129489
StephenMolloy wants to merge 1 commit into
dotnet:mainfrom
StephenMolloy:1398-xmlserializer-prereq

Conversation

@StephenMolloy

@StephenMolloy StephenMolloy commented Jun 16, 2026

Copy link
Copy Markdown
Member

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.

Comment on lines +643 to +646
var actual = SerializeAndDeserialize(customDoc,
WithXmlHeader(@"<customElement name=""testElement"" regularAttribute=""regularValue"" customAttribute=""customValue""/>"), skipStringCompare: true);
Assert.NotNull(actual);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants