I am trying to de-serialize a XML to object but I am getting stuck at one situation. Can anyone please help me out here.
XML:
I don't know exactly what you are doing here, but there is a gotcha with deserialization (at least binary deserialization. I don't know, but I suspect that it is the same for XML serialization). On deserializing a List
or a Dictionary
, the list is filled with null
values (or default value if it is a value type) until the deserialization constructor has been exited. Only after exiting the constructor is the list filled with the actual, deserialized T
s.
This means that if you want to do something with the list, it cannot be done in the constructor. You can instead make a method which contains any work which needs to be done with the list, and as long as it is annotated with the [OnDeserializedAttribute]
attribute, it will be called after the list has been filled but before the deserialization returns. The method can have any name.
See MSDN for details.