c# - deserialize and constructor relationship -


i don't know why can't find easy quick lookup on web wondering relationship between deserializing xml representation of object , constructor object?

i assuming uses default constructor. , if that's case, run code in constructor, not update object after reflect xml?

here's bit more context on mean...

i have object 2 properties objects well:

public class deployment {     public apppoolsettings apppool { get; set; }     public websitesettings site { get; set; }      public deployment()     {         //the object constructors below init internal properties well...         this.apppool = new apppoolsettings();         this.site = new websitesettings();     } } 

the problem having in xml, apppool property can null (say, if you're deploying html package). serialization procedure works properly, say, resulting xml contains entry site, , no entry apppool.

however, when deserialize xml, apppool property of deployment object instantiated , initialized... not xml saying.

am doing wrong or because of default constructor?

see would've expected deserializer perform tasks in order:
1- call default constructor
2- apppool property exist in xml?
       yes --> fill,
       no --> set null
3- site property exist in xml?
       yes --> fill,
       no --> set null

why not doing that?

i believe correct answer is: yes, counterintuitive handling of null properties (omitting them in serialized data , doing nothing them on deserialization) feature of xmlserializer. can override behavior , force xmlserializer write nulls xml attributes that:

public class deployment {     [xmlelement(isnullable = true)]     public apppoolsettings apppool { get; set; }     [xmlelement(isnullable = true)]     public websitesettings site { get; set; }      public deployment()     {         //the object constructors below init internal properties well...         this.apppool = new apppoolsettings();         this.site = new websitesettings();     } } 

then you'll <apppool xsi:nil="true" /> in xml , expected deserialization.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -