Deserialization of invalid enum values

S

steinmr

I have a class with an enum field which is serialized. For whatever
reason, the enum value that was serialized, no longer exists in the
enum. When I deserialize, I get an InvalidOperationException. Is there
any way of ignoring this error in the xml document, set the enum to
the default value, and continue deserialization?

Sample program below, throws an exception on the last line, but not if
the line before it is removed...

public class Program
{
public class MyClass
{
public MyEnum MyEnumField = MyEnum.MyEnumValue;
}
public enum MyEnum
{
MyEnumValue
}
static void Main(string[] args)
{
MyClass myClass = new MyClass();
System.Xml.Serialization.XmlSerializer xmlSerializer
= new System.Xml.Serialization.XmlSerializer(typeof(MyClass));
System.IO.StringWriter stringStream = new System.IO.StringWriter();
xmlSerializer.Serialize(new System.Xml.XmlTextWriter(stringStream),
myClass);
string serializedString = stringStream.ToString();

// Deserialize after modification
serializedString = serializedString.Replace("MyEnumValue",
"AnInvalidEnumValue");
MyClass deserializedClass
= (MyClass)xmlSerializer.Deserialize(
new System.Xml.XmlTextReader(new
System.IO.StringReader(serializedString)));
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

The only way I can think of to get around this is to implement
IXmlSerializable and handle the serialization/deserialization yourself,
ignoring the enum value if it doesn't exist anymore.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Not that I know of. After all it's an error!

The only workaround I see is to mark the item as not serializable and
instead serialize an integer representation . then after you reconstruct the
object you can decide what to do.
Or even better, instead of having a member of enum type has it of integer
and create a pblic property of type Enum. there in the get you convert it to
int, if the int stored is not longer in the enm you will get an error and
you must handle it accordingly.
 
S

steinmr

Thank you for your answers. I was hoping there was an easier path than
IXmlSerializeable, as the rest of the class does not need special
treatment.
Serializing as int is an option, but not ideal (I would like to keep
my enums int-free...)

I was hoping for an XmlIgnoreError-attribute or something like that,
but I guess I have to live with it...


Hi,

Not that I know of. After all it's an error!

The only workaround I see is to mark the item as not serializable and
instead serialize an integer representation . then after you reconstruct the
object you can decide what to do.
Or even better, instead of having a member of enum type has it of integer
and create a pblic property of type Enum. there in the get you convert it to
int, if the int stored is not longer in the enm you will get an error and
you must handle it accordingly.


I have a class with an enum field which is serialized. For whatever
reason, the enum value that was serialized, no longer exists in the
enum. When I deserialize, I get an InvalidOperationException. Is there
any way of ignoring this error in the xml document, set the enum to
the default value, and continue deserialization?
Sample program below, throws an exception on the last line, but not if
the line before it is removed...
public class Program
{
public class MyClass
{
public MyEnum MyEnumField = MyEnum.MyEnumValue;
}
public enum MyEnum
{
MyEnumValue
}
static void Main(string[] args)
{
MyClass myClass = new MyClass();
System.Xml.Serialization.XmlSerializer xmlSerializer
= new System.Xml.Serialization.XmlSerializer(typeof(MyClass));
System.IO.StringWriter stringStream = new System.IO.StringWriter();
xmlSerializer.Serialize(new System.Xml.XmlTextWriter(stringStream),
myClass);
string serializedString = stringStream.ToString();
// Deserialize after modification
serializedString = serializedString.Replace("MyEnumValue",
"AnInvalidEnumValue");
MyClass deserializedClass
= (MyClass)xmlSerializer.Deserialize(
new System.Xml.XmlTextReader(new
System.IO.StringReader(serializedString)));
}
}
 
Top