Expert Texture Home Contact me About Subscribe Digipede Connect on LinkedIn rwandering on Twitter rwandering on FriendFeed

rwandering.net

The blogged wandering of Robert W. Anderson

XmlSerializer and ObsoleteAttribute redux

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.

Tags: ,

    Trackback

1 Comment »

    Jim Benson wrote @ August 18th, 2006 at 5:57 pm

No matter how hard I try. No matter how much I work for it. I will never outgeek you.

Your comment

HTML-Tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>