Obsolete attribute causes property to be ignored by XmlSerialization

前端 未结 6 1058
臣服心动
臣服心动 2020-12-05 12:54

I\'m refactoring some objects that are serialized to XML but need to keep a few properties for backwards compatibility, I\'ve got a method that converts the old object into

6条回答
  •  离开以前
    2020-12-05 13:07

    EDIT: After reading a MS Connect article, it appears that .Net 2.0 has a 'feature' where it makes ObsoleteAttribute equivalent to XmlIgnoreAttribute without any notification in the documentation. So I'm going to revise my answer to say that the only way to have your cake and eat it too in this instance is to follow @Will's advice and implement serialization manually. This will be your only future proof way of including Obsolete properties in your XML. It is not pretty in .Net 2.0, but .Net 3.0+ can make life easier.

    From XmlSerializer:

    Objects marked with the Obsolete Attribute no longer serialized In the .NET Framework 3.5 the XmlSerializer class no longer serializes objects that are marked as [Obsolete].

提交回复
热议问题