views:

58

answers:

1

With DataContracts you can derive from IExtensibleDataObject to allow round-tripping to work without losing any unknown additional data from your XML file.

I can't use DataContract because I need to control the formatting of the output XML. But I also need to be able to read a future version of the XML file in the old version of the app, without losing any of the data from the XML file.

e.g.

XML v1:

<Person>
    <Name>Fred</Name>
</Person>

XML v2:

<Person>
    <Name>Fred</Name>
    <Age>42</Age>
</Person>

If reading an XML v2 file from v1 of my app, deserializing and serializing it again turns it into an XML v1 file. i.e. the "Age" field is erased.

Is there anything similar to IExtensibleDataObject that I can use with XmlSerializer to avoid the Age field disappearing?

+1  A: 

[XmlAnyAttribute] and [XmlAnyElement].

Marc Gravell
Thanks, exactly what I wanted.I added the following property to each of my serializable classes:[XmlAnyElement] public XmlElement Anything;
demoncodemonkey
Oops that should be: [XmlAnyElement] public XmlElement[] Anything;
demoncodemonkey