Skip to content

Reflection-based XmlSerializer resolves [XmlChoiceIdentifier] enum incorrectly with [XmlEnum] aliases #130017

Closed

Description

@StephenMolloy

Note

This issue was authored with the assistance of GitHub Copilot. The analysis and repro were verified against the dotnet/runtime build.

Description

The reflection-based XmlSerializer reader (ReflectionXmlSerializationReader) resolves the [XmlChoiceIdentifier] enum value incorrectly when the choice enum uses [XmlEnum] aliases (i.e., the enum member's C# identifier differs from the XML element name). In that case it leaves the choice identifier member at its default value instead of the value that corresponds to the element actually read.

The ILGen reader (XmlSerializationReaderILGen) and the C# code-gen reader (XmlSerializationReaderCodeGen) handle this correctly. So the behavior depends on which serialization backend is in use for the same public API, same types, and same XML.

This is independent of, and was discovered while working on, #1401.

Reproduction

public enum AliasedChoiceType
{
    [XmlEnum("Word")]
    WordChoice,
    [XmlEnum("Number")]
    NumberChoice,
}

public class AliasedChoiceContainer
{
    [XmlChoiceIdentifier("ChoiceType")]
    [XmlElement("Word", typeof(string))]
    [XmlElement("Number", typeof(int))]
    public object Item;

    [XmlIgnore]
    public AliasedChoiceType ChoiceType;
}

Deserialize:

<?xml version="1.0"?>
<AliasedChoiceContainer><Number>42</Number></AliasedChoiceContainer>

Expected

Item == 42 and ChoiceType == AliasedChoiceType.NumberChoice (the element read was <Number>).

Actual

  • ILGen reader: ChoiceType == NumberChoice ✔ (correct)
  • Reflection reader: ChoiceType == WordChoice ✘ (left at default(AliasedChoiceType), which is the first member)

Item is deserialized correctly in both readers; only the choice-identifier member is wrong in the reflection reader.

Round-trip self-inconsistency

This is not a "stricter but defensible" difference — the reflection serializer does not round-trip with its own writer. ReflectionXmlSerializationWriter emits <Number> for ChoiceType == NumberChoice, but the reflection reader then reads <Number> back as WordChoice. Serialize-then-deserialize through the same serializer silently changes the value.

Root cause

ChoiceIdentifierAccessor.MemberIds[i] is populated by XmlReflectionImporter (around XmlReflectionImporter.cs:1992) to hold the enum member's C# identifier (Constants[j].Name), position-aligned to Elements[i] by matching on the constant's XmlName.

  • The codegen readers index this position-aligned array: choice.MemberIds[elementIndex] (e.g. XmlSerializationReaderILGen.cs:3264, XmlSerializationReader.cs:4929). The matched element's index yields the correct constant identifier regardless of aliasing.
  • The reflection reader instead compares each MemberIds entry (a C# identifier) directly against the incoming XML element name (ReflectionXmlSerializationReader.cs, member.ChoiceSource around line 1786):
foreach (var name in choice.MemberIds!)
{
    if (name == elementName)   // comparing identifier against XML element name
    {
        object choiceValue = Enum.Parse(choice.Mapping!.TypeDesc!.Type!, name);
        SetOrAddValueToMember(o, choiceValue, choice.MemberInfo!);
        break;
    }
}

This only works when the enum identifier happens to equal the XML element name. Under [XmlEnum] aliasing it matches nothing, so the choice member is never set.

Suggested fix

Make the reflection reader's ChoiceSource match the incoming element name against each constant's XmlName (mirroring ReflectionXmlSerializationWriter.FindChoiceEnumValue, around ReflectionXmlSerializationWriter.cs:300) and assign the corresponding Constants[j].Name. The codegen readers need no change.

Notes

  • Only the reflection-based reader is affected. NativeAOT and Mode.ReflectionOnly scenarios use this reader.
  • The common case where enum members are named identically to their XML elements works in all backends; only aliased (or otherwise non-identifier element names) trigger the bug.

Activity

  1. added
    untriaged
    on Jun 30, 2026
  2. added this to the 11.0.0 milestone on Jun 30, 2026
  3. added
    in-pr
    and removed
    untriaged
    on Jul 10, 2026
  4. added a commit that references this issue on Jul 27, 2026
    39aaf28
  5. locked and limited conversation to collaborators on Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Serializationin-pr

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions