I’ve got a non standard XML that include a template (default values) and specified fields. As an example is always welcomed :
<MyClass>
<ArrayOfSubClass>
<SubClass>
<Par1>1</Par1>
<Par2>2</Par2>
<Par3>3</Par3>
<ArrayOfSpecific>
<Specific>
<Par1>11</Par1>
</Specific>
</ArrayOfSpecific>
</SubClass>
</ArrayOfSubClass>
</MyClass>
I want to deserialize this class MyClass into an object. As you can see, MyClass is an collection of classes SubClass which has three parameters and a collection of classes Specific. Classes SubClass and Specific are derived from the same mother class.
What ISpecific I would like to find the default values (ie the values found in SubClass) unless a field is given. What I did so far is implementing a method which by reflection checks if the property of the class Specific has a default value of the property type, and replace it by the property of the SubClass if it is the case. It works very well but I don’t cover all the cases. For example, imagine I have a value of the double property Par1 in the SubClass, say Par1 = 1.234, but I want it to be now 0. The problem is that 0 is the default value of type double, so with my method I would retrieve the value of the property in the SubClass.
The best would have been to deserialize the MyClass object first, then put all properties of classes Specific with the value of the same property in SubClass, and then deserialize again into this existing object which would change only properties that are given in the XML file.
It is quite tricky so and I can imagine that my question is not easy to understand…
[Edit:] To try to be more understandable, here is the result I’d like once deserialized
<MyClass>
<ArrayOfSubClass>
<SubClass>
<Par1>1</Par1>
<Par2>2</Par2>
<Par3>3</Par3>
<ArrayOfSpecific>
<Specific>
<Par1>11</Par1>
<Par2>2</Par2>
<Par3>3</Par3>
</Specific>
</ArrayOfSpecific>
</SubClass>
</ArrayOfSubClass>
</MyClass>
It is done by reflection after deserialization, if the property of Specific has a default value, then take the value of the class SubClass. The tricky case is as follow
<MyClass>
<ArrayOfSubClass>
<SubClass>
<Par1>1</Par1>
<Par2>2</Par2>
<Par3>3</Par3>
<ArrayOfSpecific>
<Specific>
<Par1>0</Par1> <----- 0 is the default value of a double
</Specific>
</ArrayOfSpecific>
</SubClass>
</ArrayOfSubClass>
</MyClass>
Presently the result would be
<MyClass>
<ArrayOfSubClass>
<SubClass>
<Par1>1</Par1>
<Par2>2</Par2>
<Par3>3</Par3>
<ArrayOfSpecific>
<Specific>
<Par1>1</Par1> <---- as 0 is the default value of a double, this property is set to the value of the SubClass
<Par2>2</Par2>
<Par3>3</Par3>
</Specific>
</ArrayOfSpecific>
</SubClass>
</ArrayOfSubClass>
</MyClass>
but I want
<MyClass>
<ArrayOfSubClass>
<SubClass>
<Par1>1</Par1>
<Par2>2</Par2>
<Par3>3</Par3>
<ArrayOfSpecific>
<Specific>
<Par1>0</Par1>
<Par2>2</Par2>
<Par3>3</Par3>
</Specific>
</ArrayOfSpecific>
</SubClass>
</ArrayOfSubClass>
</MyClass>
Ok, I’ve found a workaround that’s solve my issue. I do the thing in two (in fact three) steps. First I deserialize the XML into my class. Second, I put all properties of the
Specificclasses to default values that I find in theSubClassby reflection, expect one field (the If of theSpecificclass). And third, I reload the XML into aDataSet. There, I look at theDataTablenamedSpecific, and for all the properties of my class I look if there is any column of the same name, and if the cell contains a value I put it in my class.Ouffffff !!!!
Not really beauty, but it works.
And Voila