I am baffled with the behavior of the DataContractSerializer. Our configuration is XML based. XML is used as source for DataContractSerializer.ReadObject method. Recently I have encountered a problem when some properties of deserialized object were not set. I have tracked the changes and discovered that those properties were added into XML manually. Which is OK in my opinion. Apparently it was not OK in DataContractSerializer’s opinion because it appears it expects XML nodes to be ordered alphabetically. Really?! Deserialization seems like really straightforward thing – read XML sequentially, parse node name, set corresponding property. What’s the purpose of ordering?
Is there a workaround? Maybe some sort of settings for DataContractSerializer?
I encountered this problem recently. To work around it, I used the
XmlSerializerand removed the explicit ordering from theXmlElementattributes:There’s some more information on my blog post.
If you want to know why the order matters, it’s because a
sequencein XSD has a defined order, and the web service contracts are defined with XSD.From the specification: