InvalidOperationException with the XmlSerializer in .Net 4.0

There is a small change in the XmlSerializer type that might break your code then moving to .net 4.0. MSDN says: “In the the XmlSerializer class no longer serializes objects that are marked as [Obsolete].”. Not only is this not grammatically ok, this means: if you have objects in your entities that are marked as obsolete, they cannot be serialized to xml any more.

This also means: DO NOT USE THIS ATTRIBUTE IN ENUMS.

The simple reason: if you have an old serialized entity with an enum value that is obsolete, you can’t even load it. The error message will tell you that the value of {some enum member name} is not a valid value for that type; so you will need to write “cleanup code” while serializing / deserializing entities to be able to use the entities in serialization again – you have to implement IXmlSerializable in order to get that working.

This also means that the “Obsolete” attribute is no longer a “hint” – it does have impact to the execution of your code and may break existing code.