Serialize enum as int

I want to return the next class through a web service that includes an enumeration type as one of its members.

[Serializable, XmlRoot("GeoCoordinate")] public class GeoCoordinate { public enum AccuracyLevel { Unknown = 0, Country = 1, Region = 2, SubRegion = 3, Town = 4, PostalCode = 5, Street = 6, Intersection = 7, Address = 8, Premise = 9 } private AccuracyLevel _accuracy; // ... more members public AccuracyLevel Accuracy { get { return _accuracy; } set { _accuracy = value;} } } 

This works correctly, but returns the result as:

 <!-- ... --> <Accuracy>Unknown or Country or Region or SubRegion or Town or PostalCode or Street or Intersection or Address or Premise</Accuracy> <!-- ... --> 

Instead of a string representing an enumeration, I would just like to return an integer. Can this be done without changing the type of GeoCoordinate.Accuracy ?

+6
enums xml-serialization
source share
3 answers

Although this is a hack, I believed that the XmlEnumAttribute for each member of the enumeration would be the most paid in this case. If this enumeration was much larger, it would be better to use XmlIgnore in the Accuracy property and add an additional int property to the class, as described in another answer to this question.

Usng XmlEnumAttribute means that you only need to change the enumeration name and xml will be serialized as an int, wherever it is used.

  public enum AccuracyLevel { [XmlEnum("0")] Unknown = 0, [XmlEnum("1")] Country = 1, [XmlEnum("2")] Region = 2, [XmlEnum("3")] SubRegion = 3, [XmlEnum("4")] Town = 4, [XmlEnum("5")] PostalCode = 5, [XmlEnum("6")] Street = 6, [XmlEnum("7")] Intersection = 7, [XmlEnum("8")] Address = 8, [XmlEnum("9")] Premise = 9 } 
+4
source share

I believe that you need to use [XmlIgnore] in the enumeration and create a second property that returns an integer value:

 [XmlRoot("GeoCoordinate")] public class GeoCoordinate { public enum AccuracyLevel { Unknown = 0, Country = 1, Region = 2, SubRegion = 3, Town = 4, PostalCode = 5, Street = 6, Intersection = 7, Address = 8, Premise = 9 } private AccuracyLevel _accuracy; // ... more members [XmlIgnore] public AccuracyLevel Accuracy { get { return _accuracy; } set { _accuracy = value;} } [XmlElement("AccuracyLevel")] public int AccuracyLevelInt { get {return (int) AccuracyLevel;} set {AccuracyLevel = (AccuracyLevel) value;} } } 

Note that [Serializable] not used by the XML serializer.

Also note that the AccuracyLevelInt property is probably not executed correctly. I am studying this now.

+3
source share

Decorate the enumeration with [Serializable] or [DataContract] . There are several differences between the two attributes, make sure you check it out ( this blog post can help with this). And mark individual enumeration elements with [EnumMember] . I have never checked what renaming looks like on the way, but this will ensure it is reached at the other end and also ensure it is received if you create a proxy server on the client side.

+1
source share

All Articles