- 
                Notifications
    
You must be signed in to change notification settings  - Fork 6.1k
 
Add breaking change documentation for XmlSerializer ObsoleteAttribute behavior in .NET 10 #49390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            gewarren
  merged 5 commits into
  main
from
copilot/fix-xml-serialization-obsolete-attribute
  
      
      
   
  Oct 22, 2025 
      
    
  
     Merged
                    Changes from 2 commits
      Commits
    
    
            Show all changes
          
          
            5 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      49632b3
              
                Initial plan
              
              
                Copilot 36f6a86
              
                Add breaking change doc for XmlSerializer ObsoleteAttribute behavior
              
              
                Copilot cc94f55
              
                Apply suggestions from code review
              
              
                gewarren f1bc4db
              
                Rename serialization/10.0 directory to serialization/10
              
              
                Copilot 0a6b4f5
              
                Apply suggestions from code review
              
              
                gewarren File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            123 changes: 123 additions & 0 deletions
          
          123 
        
  docs/core/compatibility/serialization/10.0/xmlserializer-obsolete-properties.md
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| --- | ||
| title: "Breaking change: XmlSerializer no longer ignores properties marked with ObsoleteAttribute" | ||
| description: "Learn about the breaking change in .NET 10 where properties marked with ObsoleteAttribute are now serialized by XmlSerializer instead of being ignored." | ||
| ms.date: 10/21/2025 | ||
| ai-usage: ai-assisted | ||
| ms.custom: https://github.com/dotnet/runtime/issues/100453 | ||
| --- | ||
| # XmlSerializer no longer ignores properties marked with ObsoleteAttribute | ||
| 
     | 
||
| Starting in .NET 10, the behavior of <xref: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. | ||
                
      
                  gewarren marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| With this change, properties marked with `[Obsolete]` are now serialized by default unless the `IsError` property of the `[Obsolete]` attribute is set to `true`. If `IsError` is `true`, the serializer throws an <xref:System.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. | ||
                
      
                  gewarren marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| ## Version introduced | ||
| 
     | 
||
| .NET 10 Preview 1 | ||
| 
     | 
||
| ## 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. | ||
| 
     | 
||
| ```csharp | ||
| 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:** | ||
| 
     | 
||
| ```xml | ||
| <Example> | ||
| <NormalProperty>normal</NormalProperty> | ||
| </Example> | ||
| ``` | ||
| 
     | 
||
| ## New behavior | ||
| 
     | 
||
| Starting in .NET 10, 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 is serialized normally. | ||
| 2. If the `[Obsolete]` attribute is applied with `IsError = true`, the <xref:System.Xml.Serialization.XmlSerializer> throws an <xref:System.InvalidOperationException> during serializer creation. | ||
                
      
                  gewarren marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| 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. | ||
                
      
                  gewarren marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| Using the same code as above, the output after the change is: | ||
                
      
                  gewarren marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| **Output after the change (default behavior):** | ||
| 
     | 
||
                
      
                  gewarren marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| ```xml | ||
| <Example> | ||
| <NormalProperty>normal</NormalProperty> | ||
| <ObsoleteProperty>obsolete</ObsoleteProperty> | ||
| </Example> | ||
| ``` | ||
| 
     | 
||
| If the AppContext switch `Switch.System.Xml.IgnoreObsoleteMembers` is enabled, the previous behavior is restored: | ||
| 
     | 
||
| ```csharp | ||
| 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:** | ||
| 
     | 
||
| ```xml | ||
| <Example> | ||
| <NormalProperty>normal</NormalProperty> | ||
| </Example> | ||
| ``` | ||
| 
     | 
||
| If `[Obsolete(IsError = true)]` is applied to a property, the following exception is thrown during serializer creation: | ||
| 
     | 
||
| ``` | ||
| System.InvalidOperationException: Cannot serialize member 'ObsoleteProperty' because it is marked with ObsoleteAttribute and IsError is set to true. | ||
| ``` | ||
| 
     | 
||
| > [!NOTE] | ||
| > Properties that are marked as Obsolete have always successfully deserialized when data is present in the XML. While this change allows `[Obsolete]` properties to "round trip" from object to XML and back to object, the new behavior only affects the serialization half (object to XML) of the "round trip." | ||
                
      
                  gewarren marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| ## Type of breaking change | ||
| 
     | 
||
| This change is a [behavioral change](../../categories.md#behavioral-change). | ||
| 
     | 
||
| ## 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 doesn't affect runtime serialization behavior. The introduction of the AppContext switch allows developers to opt in to the legacy behavior if necessary. | ||
| 
     | 
||
| ## Recommended action | ||
| 
     | 
||
| Review your codebase 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: | ||
| 
     | 
||
| ```csharp | ||
| 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. | ||
| 
     | 
||
| ## Affected APIs | ||
| 
     | 
||
| - <xref:System.Xml.Serialization.XmlSerializer?displayProperty=fullName> | ||
| 
     | 
||
| ## See also | ||
| 
     | 
||
| - [Pull Request dotnet/runtime#119865](https://github.com/dotnet/runtime/pull/119865) | ||
| - [Related Issue dotnet/runtime#100453](https://github.com/dotnet/runtime/issues/100453) | ||
                
      
                  gewarren marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.