I am trying to deserialize an XML string where the element value is not in the scope of my Enum values.
Public enum MyEnum
{
Unknown,
Car,
Bicycle,
Boat
}
[SerializableAttribute()]
public class MyClass
{
private string _id;
private MyEnum _myEnum;
public string ID
{
get { return _id; }
set { _id = value; }
}
public MyEnum EnumValue
{
get { return _myEnum; }
set { _myEnum = value; }
}
public MyClass(string id)
{
this._id = id;
}
public MyClass() : this("") { }
}
If I try to deserialize the following line (note Plane as an enumeration value):
<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><MyClass><ID>1234567890123456789</ID><EnumValue>Plane</EnumValue></MyClass>
then my deserialization will throw an exception before it even gets into my public field for EnumValue with the following exception message:
Instance validation error: "Plane" is not a valid value for EnumValue
Is it possible to return the default value for EnumValue if the value I'm trying to parse in XML is not supported as EnumValue? For instance. in the case of the XML string provided here, the value of EnumValue shall be set to "Unknown".