Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 21, 2025

  • Explore repository structure and understand breaking change documentation pattern
  • Create serialization/10.0 directory
  • Create the breaking change document for ObsoleteAttribute XML serialization behavior
  • Update TOC file to include the new breaking change
  • Update 10.0.md overview file to add Serialization section
  • Validate document formatting and links
  • Verify document follows writing style guidelines
  • Apply code review suggestions
  • Rename folder from serialization/10.0 to serialization/10

Summary

Created comprehensive breaking change documentation for the .NET 10 change where XmlSerializer no longer ignores properties marked with ObsoleteAttribute. The document includes:

  • Clear explanation of the breaking change
  • Code examples showing old and new behavior
  • Documentation of the AppContext switch (Switch.System.Xml.IgnoreObsoleteMembers)
  • Recommended actions for developers
  • Proper cross-references to affected APIs

All code review suggestions have been applied, including renaming the directory from "serialization/10.0" to "serialization/10" to match the pattern used in other .NET 10 breaking change categories.

Original prompt

This section details on the original issue you should resolve

<issue_title>[Breaking change]: Fix ObsoleteAttribute incorrectly causing XML serialization to ignore properties with AppContext switch and SR resources</issue_title>
<issue_description>### Description

Starting in .NET 10, the behavior of the System.Xml.Serialization.XmlSerializer has changed with respect to how it handles properties marked with the [Obsolete] attribute. Previously, properties marked with [Obsolete] were treated as if they were also marked with [XmlIgnore], causing them to be excluded from XML serialization. This behavior was unintended and has been corrected.

With this change, properties marked with [Obsolete] will now be serialized by default unless the IsError property of the [Obsolete] attribute is set to true. If IsError is true, the serializer will throw an InvalidOperationException during creation. Additionally, an AppContext switch, Switch.System.Xml.IgnoreObsoleteMembers, has been introduced to allow developers to revert to the previous behavior if necessary.

Version

.NET 10

Previous behavior

In previous versions of .NET, properties marked with the [Obsolete] attribute were excluded from XML serialization, similar to properties marked with [XmlIgnore]. This behavior was unexpected and not aligned with the intended purpose of the [Obsolete] attribute, which is to provide compile-time warnings about deprecated APIs.

Example

public class Example
{
    public string NormalProperty { get; set; } = "normal";
    
    [Obsolete("This property is deprecated")]
    public string ObsoleteProperty { get; set; } = "obsolete";
    
    [XmlIgnore]
    public string IgnoredProperty { get; set; } = "ignored";
}

var obj = new Example();
var serializer = new XmlSerializer(typeof(Example));
using var writer = new StringWriter();
serializer.Serialize(writer, obj);
Console.WriteLine(writer.ToString());

Output before the change:

<Example>
  <NormalProperty>normal</NormalProperty>
</Example>

New behavior

Starting in .NET 11, properties marked with [Obsolete] are no longer excluded from XML serialization by default. Instead:

  1. If the [Obsolete] attribute is applied with IsError = false (default), the property will be serialized normally.
  2. If the [Obsolete] attribute is applied with IsError = true, the XmlSerializer will throw an InvalidOperationException during serializer creation.

An AppContext switch, Switch.System.Xml.IgnoreObsoleteMembers, has been introduced to allow developers to restore the previous behavior where [Obsolete] properties are ignored during serialization. This switch is off by default.

Example

Using the same code as above, the output after the change will be:

Output after the change (default behavior):

<Example>
  <NormalProperty>normal</NormalProperty>
  <ObsoleteProperty>obsolete</ObsoleteProperty>
</Example>

If the AppContext switch Switch.System.Xml.IgnoreObsoleteMembers is enabled, the previous behavior is restored:

AppContext.SetSwitch("Switch.System.Xml.IgnoreObsoleteMembers", true);

var obj = new Example();
var serializer = new XmlSerializer(typeof(Example));
using var writer = new StringWriter();
serializer.Serialize(writer, obj);
Console.WriteLine(writer.ToString());

Output with AppContext switch enabled:

<Example>
  <NormalProperty>normal</NormalProperty>
</Example>

If [Obsolete(IsError = true)] is applied to a property, the following exception will be thrown during serializer creation:

System.InvalidOperationException: Cannot serialize member 'ObsoleteProperty' because it is marked with ObsoleteAttribute and IsError is set to true.

Type of breaking change

  • Behavioral change: Existing binaries might behave differently at runtime.

Reason for change

The previous behavior of treating [Obsolete] as equivalent to [XmlIgnore] was unintended and inconsistent with the purpose of the [Obsolete] attribute. This change ensures that [Obsolete] is used solely for its intended purpose of providing compile-time warnings and does not affect runtime serialization behavior. The introduction of the AppContext switch allows developers to opt into the legacy behavior if necessary.

Recommended action

Developers should review their codebases for any reliance on the previous behavior where [Obsolete] properties were excluded from XML serialization. If this behavior is still desired, enable the AppContext switch Switch.System.Xml.IgnoreObsoleteMembers as follows:

AppContext.SetSwitch("Switch.System.Xml.IgnoreObsoleteMembers", true);

If any properties are marked with [Obsolete(IsError = true)] and are being serialized, update the code to either remove the [Obsolete] attribute or set IsError = false to avoid runtime exceptions.

[!NOTE]
Properties that are marked as Obsolete have alway...

Fixes #49054


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.


Internal previews

📄 File 🔗 Preview link
docs/core/compatibility/10.0.md Breaking changes in .NET 10
docs/core/compatibility/serialization/10/xmlserializer-obsolete-properties.md XmlSerializer no longer ignores properties marked with ObsoleteAttribute
docs/core/compatibility/toc.yml docs/core/compatibility/toc

Copilot AI changed the title [WIP] Fix XML serialization for obsolete properties Add breaking change documentation for XmlSerializer ObsoleteAttribute behavior in .NET 10 Oct 21, 2025
Copilot finished work on behalf of gewarren October 21, 2025 20:20
Copilot AI requested a review from gewarren October 21, 2025 20:20
@gewarren gewarren marked this pull request as ready for review October 22, 2025 00:08
Copilot AI review requested due to automatic review settings October 22, 2025 00:08
@gewarren gewarren requested a review from a team as a code owner October 22, 2025 00:08
Copy link
Contributor

Copilot AI left a comment

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 documents a breaking change in .NET 10 where XmlSerializer now serializes properties marked with [Obsolete] instead of treating them as if they were marked with [XmlIgnore]. The change includes an AppContext switch for backward compatibility and proper handling of IsError = true cases.

Key changes:

  • New breaking change documentation with comprehensive examples and migration guidance
  • AppContext switch documentation for restoring legacy behavior
  • Updated TOC and overview page to include the new Serialization section

Reviewed Changes

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

File Description
docs/core/compatibility/serialization/10.0/xmlserializer-obsolete-properties.md New breaking change article documenting XmlSerializer behavior change with code examples and migration paths
docs/core/compatibility/toc.yml Added new Serialization section under .NET 10 breaking changes
docs/core/compatibility/10.0.md Added Serialization section to the .NET 10 breaking changes overview

Copilot AI requested a review from gewarren October 22, 2025 00:18
Copilot finished work on behalf of gewarren October 22, 2025 00:18
@gewarren gewarren merged commit a818b31 into main Oct 22, 2025
10 checks passed
@gewarren gewarren deleted the copilot/fix-xml-serialization-obsolete-attribute branch October 22, 2025 18:58
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.

[Breaking change]: Fix ObsoleteAttribute incorrectly causing XML serialization to ignore properties with AppContext switch and SR resources

3 participants