locked
Is it possible to preserve enum semantics over XmlSerialization, when non-default enum values are declared server side? RRS feed

  • Question

  • User-2027985085 posted

    I've got an enum declared like so:

    public enum MyEnum

    {
    Black=-1,
    Red=0,
    Green=1,
    Blue=2
    }
    The numeric values are stored in a database.

    My problem is that on the client side (proxy auto generated code) the enum no longer has the non-default values assigned. Therefore it effectively becomes:

    public enum MyEnum

    { Black=0,
    Red=1,
    Green=2,
    Blue=3 }

    But the value that actually travels across the http wire is the numeric! So all the values become misinterpreted(!!!!)

    I need the database to record numeric, and either the proxy code to retain the non-default values, or for the http to pass the labels instead of the values.

    Any tips much appreciated.

    Kind Regards

    Martin

    Tuesday, October 21, 2014 12:31 PM

Answers

  • User-2027985085 posted

    Solved - somewhere deep in the code there were properties that were declared as int rather than the enum (Doh!! & ouch)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2014 3:52 AM

All replies

  • User-2027985085 posted

    I am surprised to see the values going the http wire as numeric values, when the wsdl declares them as with a type like this:

    <s:simpleType name="MyEnum">
    <s:restriction base="s:string">
    <s:enumeration value="Black" />
    <s:enumeration value="Red" />
    <s:enumeration value="Green" />
    <s:enumeration value="Blue" />
    </s:restriction>
    </s:simpleType>

    To my mind the only valid values to go over the wire are the literals Black, Red, Green and Blue. Anything else should fail xml validation. It appears the XML is not validated against the auto generated schema in the wsdl???

    PS I am using .Net 2.0 and asmx

    Tuesday, October 21, 2014 1:28 PM
  • User-2027985085 posted

    Solved - somewhere deep in the code there were properties that were declared as int rather than the enum (Doh!! & ouch)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2014 3:52 AM
  • User-97501952 posted

    Hi,

    Thanks for sharing your solution.

    May

    Wednesday, October 22, 2014 4:27 AM