I am trying to deserialize an Xml document to a C# class. The Xml looks something like this:
<response>
<result>Success</result>
</response>
That result can be only "Success" or "Failed". When I deserialize it I want to put the value into a bool with "Success" = true and "Failed" = false. I can't quite figure out how to set the true and false constants though? The code I have at the moment looks like this.
[XmlRoot(ElementName="response")]
public class Response()
{
[XmlElement(ElementName="result")]
public bool Result { get; set; }
}
Define another property that is hidden, which does the translation for you:
[XmlRoot(ElementName="response")]
public class Response()
{
[XmlElement(ElementName="result")]
private string ResultInternal { get; set; }
[XmlIgnore()]
public bool Result{
get{
return this.ResultInternal == "Success";
}
set{
this.ResultInternal = value ? "Success" : "Failed";
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With