From 0175e3f80af34d6b7db154c47a129986be7c5c6a Mon Sep 17 00:00:00 2001 From: Steve Molloy Date: Tue, 16 Jun 2026 15:14:53 -0700 Subject: [PATCH] Implement support for XmlAttribute in WriteAttribute method and add corresponding test --- .../ReflectionXmlSerializationReader.cs | 9 +++++--- .../XmlSerializerTests.Internal.cs | 21 ------------------- .../tests/XmlSerializer/XmlSerializerTests.cs | 20 ++++++++++++++++++ 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs index 0c08561ba20ced..c02671e03fd570 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationReader.cs @@ -1981,9 +1981,12 @@ private void WriteAttribute(Member member, object? attr = null) } else if (special.TypeDesc.CanBeAttributeValue) { - // https://github.com/dotnet/runtime/issues/1398: - // To Support special.TypeDesc.CanBeAttributeValue == true - throw new NotImplementedException("special.TypeDesc.CanBeAttributeValue"); + if (attr is not XmlAttribute xmlAttribute) + { + return; + } + + value = xmlAttribute; } else throw new InvalidOperationException(SR.XmlInternalError); diff --git a/src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.Internal.cs b/src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.Internal.cs index 733b5ae83c1952..14eec7cd191383 100644 --- a/src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.Internal.cs +++ b/src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.Internal.cs @@ -11,27 +11,6 @@ public static partial class XmlSerializerTests { - // Move this test to XmlSerializerTests.cs once #1398 is fixed for the ReflectionOnly serializer. - [Fact] - public static void Xml_CustomDocumentWithXmlAttributesAsNodes() - { - var customDoc = new CustomDocument(); - var customElement = new CustomElement() { Name = "testElement" }; - customElement.AddAttribute(customDoc.CreateAttribute("regularAttribute", "regularValue")); - customElement.AddAttribute(customDoc.CreateCustomAttribute("customAttribute", "customValue")); - customDoc.CustomItems.Add(customElement); - var element = customDoc.Document.CreateElement("regularElement"); - var innerElement = customDoc.Document.CreateElement("innerElement"); - innerElement.InnerXml = "innerText"; - element.InnerText = "regularText"; - element.AppendChild(innerElement); - element.Attributes.Append(customDoc.CreateAttribute("regularElementAttribute", "regularElementAttributeValue")); - customDoc.AddItem(element); - var actual = SerializeAndDeserialize(customDoc, - WithXmlHeader(@""), skipStringCompare: true); - Assert.NotNull(actual); - } - // Move this test to XmlSerializerTests.cs once #1401 is fixed for the ReflectionOnly serializer. [Fact] public static void Xml_DerivedIXmlSerializable() diff --git a/src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs b/src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs index 1451e7407d8148..808120428c809a 100644 --- a/src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs +++ b/src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs @@ -625,6 +625,26 @@ public static void Xml_XmlAnyAttributeTest() } } + [Fact] + public static void Xml_CustomDocumentWithXmlAttributesAsNodes() + { + var customDoc = new CustomDocument(); + var customElement = new CustomElement() { Name = "testElement" }; + customElement.AddAttribute(customDoc.CreateAttribute("regularAttribute", "regularValue")); + customElement.AddAttribute(customDoc.CreateCustomAttribute("customAttribute", "customValue")); + customDoc.CustomItems.Add(customElement); + var element = customDoc.Document.CreateElement("regularElement"); + var innerElement = customDoc.Document.CreateElement("innerElement"); + innerElement.InnerXml = "innerText"; + element.InnerText = "regularText"; + element.AppendChild(innerElement); + element.Attributes.Append(customDoc.CreateAttribute("regularElementAttribute", "regularElementAttributeValue")); + customDoc.AddItem(element); + var actual = SerializeAndDeserialize(customDoc, + WithXmlHeader(@""), skipStringCompare: true); + Assert.NotNull(actual); + } + [Fact] public static void Xml_Struct() {