We can then define a class that uses it:
When we serialize an instance of the class, with either .Net 2.0 serialization or data contract serialization, we get something like this:
assuming the active flags are Admins and RemoteUsers.
If we deserialize this back to the class everything works fine. But what if the xml needs to be manually parsed for some reason and we need to do something like this:
We would get this exception:
The reason is that Enum.Parse expects a comma between the values and not a space. So this fails:
while this work:
Since a space is not valid inside an enum member we can safely do this when reading from the xml:
Which will work.
However in the general case manually deserializing a value is dangerous. For example the enum may contain serialization flags:
This will affect the resulting xml:
and Enum.Parse will fail. So it is always better to let whoever generated the xml from the first place to eat its own dog food and deserialize it back. What's next? get this blog rss updates or register for mail updates!
0 comments:
Post a Comment