I’m serializing my DTO to XML messages using DataContractSerializer, however I need to support 2 XML formats, they are identical in structure but differ in element naming, so I need to support an alternative set of DataMemeber[Name=””] attributes on my DTO’s. How to solve this without cloning my DTO classes? I need to support both formats at runtime, so preproccessor derectives will not suffice.
I’m serializing my DTO to XML messages using DataContractSerializer, however I need to support
Share
Firstly, for fine-grained xml serialization,
XmlSerializeris preferable toDataContractSerializer(which is fine as a general purpose serializer, but is hard to take seriously as an xml serializer when it can’t even handle attributes).Secondly,
XmlSerializerhas options for this – in particalar,XmlAttributeOverrides. With anXmlAttributeOverridesyou can configure the entire setup for your type at runtime, then just pass it in to theXmlSerializerconstructor. Important warning: do this once and store the serializer instance, otherwise you will haemorrhage dynamic assemblies.with output: