I am working on a project where I have very specific xml serialization specifications, that is:
Some properties are elements, some are attributes
Some properties are required others aren't
Some properties are required just in some context of the overall object state
Not required properties must not be serialized
Properties of same type can be serialized differently
Some
double
properties should have rounding, maybe even custom rounding
And the list goes on...
I am currently implementing these rules using Attribute
which is the only sane way.
But I wonder, what is the best way to serialize these objects? Certainly the approach that makes the most sense is to have a serializer which knows all these rules and how to handle them just as XmlSerializer
, although it seems IXmlSerializable
was created for this reason.
So, what would you do? Custom serializer or IXmlSerializable
?
IXmlSerializable
assumes that a class can (de-)serialize itself without the need for additional contextual information. Is that true for all classes that you might want to serialize?IXmlSerializable
.