Recently I complained about how the XmlSerializer
treats the ObsoleteAttribute
.
My desire was to deprecate a property with ObsoleteAttribute
without it being ignored by the XmlSerializer
. I still think that the attribute should be ignored for serialization, but here is a workaround:
Before:
[XmlAttribute] public bool A { ... }
After:
[Obsolete("This property has been deprecated; use property B instead.")] [XmlIgnore] public bool A { ... } [XmlAttribute("A")] [EditorBrowseable(EditorBrowseableState.Never)] public bool DeserializingA { ... } [XmlAttribute] [DefaultValue(false)] public bool B { ... }
The ObsoleteAttribute tells the compiler to issue warnings to developers about the old property (and, as I said above, the XmlSerializer also ignores it).
The XmlAttribute.Name
property directs the serializer to read a property from an XML attribute with a different name.
The EditorBrowseableAttribute
hides the deserializing property from Intellisense. You can also hide it from the debugger using the DebuggerBrowseableAttribute
(not shown).
The example shows this for XML attributes, but the same approach will work for other XML elements.
Not too much extra work, though I would still prefer the XmlSerializer
to just ignore the ObsoleteAttribute
.