Serialize an Enumeration as it's Underlying Type RRS feed

  • Question

  • I have a custom class that I want to serialize. The class has 4 properties: 1 String, 2 Enum of Byte, 1 Boolean. I am having no trouble doing the serialization, except that I want the enumerations to be serialized as Bytes rather than Strings (I want to see a 1, 2, 3, 4, etc. in the XML rather than the enumeration text). My main reason for this is that I want the XML to be as short as possible, so this will save a reasonable amount of space. Can anybody tell me how to do this? I know about the IXmlSerializable Interface, as well as the XmlEnum attribute, but these both involve manual conversions or manually labeling the enumeration if it gets changed. I would also be happy (actually even prefer) to do a binary enumeration, but I don't know how to do that. Can somebody help me figure out how to have an enumeration serialized as a Byte rather than a String? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Sunday, November 24, 2013 1:24 AM

All replies

  • Another option to consider is adding a 'helper' property that gets/sets the 'real' enum property via a byte (or integer). You would then tag the 'real' enum property to not be serialized, leaving the 'helper' property to be serialized as a byte.

    It's a little extra coding up front, but no maintenance.

    Good luck!

    - HomeGrownCoder My posts are kept as simple as possible for easier understanding. In many cases you can probably optimize or spruce up what I present. Have fun coding!

    Sunday, November 24, 2013 4:26 AM
  • That's an interesting idea, but it's still not what I want. But it is definitely a good solution if there is no built-in way to specify what the enumeration is serialized as.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Sunday, November 24, 2013 7:12 PM