This is duplicate of Can XmlSerializer deserialize into a Nullable<int>? but I need a solution that neither change xml document nor forces me to implement IXmlSerializable interface. I dont want to implement IXmlSerializable because I have many additional elements beside <number
> that get deserialized correctly.
My xml can contain either element <number>4</number>
or <number/>
<root>
...
either <number>4</number> or <number/>
... [other elements]
</root>
Class
public class root
{
public int? number {get; set;}
...
}
does not work.
You could always do a string replace on the final xml output.
Generally, it is a bad idea to try to hack at xml with string manipulation, but the replace above is safe and will always convert
<anything i:nil="true"/>
to<anything/>
.It's a hack, but an acceptable one considering the alternative.
You can just use a surrogate property.