Is it possible to deserialize this XML into an object marked with the DataContract attribute?
<root>
<distance units="m">1000</distance>
</root>
As you may see there is "units" attribute. I don't believe that's supported. Or am I wrong?
Is it possible to deserialize this XML into an object marked with the DataContract attribute?
<root>
<distance units="m">1000</distance>
</root>
As you may see there is "units" attribute. I don't believe that's supported. Or am I wrong?
The Data Contract Serializer used by default in WCF does not support XML attributes for performance reasons (the DCS is about 10% faster on average than the XML serializer).
So if you really want to use the DCS, you cannot use this structure you have - it would have to be changed.
Or you need to use the XmlSerializer with WCF, as Greg showed in his answer - that works, too, but you lose the performance benefit (plus all other benefits) of the DCS.
This can be achieved, but you will have to override the default serializer by applying the
[XmlSerializerFormat]
attribute to the DataContract. Although it can be done, this does not perform as well as the default serializer, so use it with caution.The following class structure will give you the result you are after:
You can test this with the following code:
The output will be: