I have a class in which I had to change te type of a property from a simple List<string>
to a complex List<CustomObject>
.
My problem is that for some period of time, I will have people using the old and the new version of the software. Up until now, when I had contract changes, I simply used the UnknownElement
event to map the old member to the new one since it was private files and it works perfectly for backward compatibility but broke the old version since it didn't write the old format back.
But this time, it is a shared file and it made me realise that I missed upward compatibility and that people using the old version will remove the new member. I read about XmlAnyElementAttribute
to keep unknown elements and have them serialized back to the file. This fixes upward compatibility.
I now have every pieces of the puzzle but I can't find how to have them work together since adding XmlAnyElementAttribute
seems to end in UnknownElement
not being triggered.
I also thought of simply reading back the XmlAnyElementAttributeproperty once the deserialization is done but this time, it is the
XmlSerializer` that lacks an event for Deserialized.
Here is a sample of both files: Old format:
<OptionsSerializable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ListeCategories>
<string>SX00</string>
<string>SX01</string>
</ListeCategories>
</OptionsSerializable>
New Format:
<OptionsSerializable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ListeCategoriesExt>
<CategoryInfo Name="SX00" Type="Principal" Persistence="Global">
<ToolTip>SX00</ToolTip>
<SearchTerm>SX00</SearchTerm>
</CategoryInfo>
<CategoryInfo Name="SX01" Type="Principal" Persistence="Global">
<ToolTip>SX01</ToolTip>
<SearchTerm>SX01</SearchTerm>
</CategoryInfo>
</ListeCategoriesExt>
</OptionsSerializable>
Needed:
<OptionsSerializable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ListeCategories>
<string>SX00</string>
<string>SX01</string>
</ListeCategories>
<ListeCategoriesExt>
<CategoryInfo Name="SX00" Type="Principal" Persistence="Global">
<ToolTip>SX00</ToolTip>
<SearchTerm>SX00</SearchTerm>
</CategoryInfo>
<CategoryInfo Name="SX01" Type="Principal" Persistence="Global">
<ToolTip>SX01</ToolTip>
<SearchTerm>SX01</SearchTerm>
</CategoryInfo>
</ListeCategoriesExt>
</OptionsSerializable>