Bug In the .NET CompactFramework XmlEnum with Whitespace
December 21, 2006
4 minute read
In .NET you can use Attributes to mark up properties in a class to tell the XmlSerializer how to marshal that class to and from XML. There are a number of different attributes you can use. You can use enums for restricted lists of values.
To represent something like Some Value
At least that's the idea...
It seems like XmlEnum marked values with whitespace in their names will not be deserialized on the .NET CF 2.0. It throws an InvalidOperationException saying it can't find the enum with value of the part of the string before the space.
I would expect it to deserialize into the enum Foo.Some. All of this works on the Full Framework.
If you control the schema you can workaround this issue by changing the allowed values not to have spaces in them. But in many cases you might be trying to integrate with an existing service or schema that you do not control and as such, you won't have the ability to change it. If that's the case what do you do?
I made some slight modifications to it, so that it was a little safer (I think).
I also needed to go the other way then, and convert a String back into an Enum so that I can support deserializing from XML into the object model.
Using this code, you can offer an API to client code that uses the strongly-typed Enum, but does a manual conversion for the serialization process.
One big workaround for what seems to me to be a bug. But it works, and I can continue to get work done. It should also be easy to refactor this workaround out if the bug ever gets fixed. Just change the [XmlIgnore] tag and delete the ForSerializer methods.
I hope that helps if you run into a similar problem.