Deserialize XML element presence to bool in C# Deserialize XML element presence to bool in C# xml xml

Deserialize XML element presence to bool in C#


One way to do it would be to use a different property to get the value of the element, then use the Target property to get whether that element exists. Like so.

[XmlElement("target", IsNullable = true)]public string TempProperty { get; set; }[XmlIgnore]public bool Target{    get    {        return this.TempProperty != null;    }}

As even if an empty element exists, the TempProperty will not be null, so Target will return true if <target /> exists


Can you explain why you dont want to use nullable types?
When u define an int (as opposed to int?) property in ur poco, it doesnt really represent the underlying xml, and u will simply get the default values for those variables.
IF u assume you wont get empty/null strings or integers with the value 0 in ur xml, youcan used the method Balthy suggested for each of ur properties, or use the method described here


Generally i think its a better idea to create a schema to describe ur xml, and generate classes based on it, while using nullable types, if you really want your classes represent the underlying data.