Deserializing to an enum

I have an application where I deserialize an xml stream from a third party. One of the fields is a task code which can be one of only three values, insert, update and delete. This is an obvious candidate for an enum.

1public enum TaskCode
2{
3    Delete,
4    Insert,
5    Update
6}

This is looks good but relies on the xml stream using the same capitalization as shown above, however my incoming xml stream used all caps. I could have changed my enums to all caps, but I’d end up with some ugly lines of code.

Fortunately the XmlEnum attribute solves this problem.

1public enum TaskCode
2{
3    [XmlEnum("DELETE")] Delete,
4    [XmlEnum("INSERT")] Insert,
5    [XmlEnum("UPDATE")] Update
6}

See http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlenumattribute.aspx for more.

comments powered by Disqus

Related