deserialize enum value as int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • slamb@cognex.com

    deserialize enum value as int

    Hi all,
    Does anyone know of a way to deserialize xml data that has an element
    that represents an enum value but is actually an int?

    I know I can use [XmlEnum(Name="2 3")] to tag enum values, that works
    but is too messy. I have many enums and tagging all values would not
    be good.


    xml data
    <Top>
    <Element>23</Element>
    </Top>

    c# code
    public enum SomeEnumType
    {
    Value1 = 23,
    Value2 = 15
    }

    public class Top
    {
    SomeEnumType Element;
    }

    SomeEnumType Deserialize(Str eam stream)
    {
    XmlSerializer xmlSerializer = new XmlSerializer(t ypeof(Top));
    object obj = xmlSerializer.D eserialize(stre am);
    Top top = obj as Top;
    if (top == null)
    throw new SomeException() ;

    return top.Element;
    }

  • Mark R. Dawson

    #2
    RE: deserialize enum value as int

    Hi Slamb,
    if you want to get an enum value from an integral type you have in your
    XML, you can use Enum.Parse. For example:

    enum MyNameEnum
    {
    Fred = 1,
    Bob = 2,
    George = 3
    }

    int intNumber = 2;
    MyNameEnum nme = (MyNameEnum)Enu m.Parse(typeof( MyNameEnum),
    intNumber.ToStr ing());


    When doing the above it is also a good idea to check that the value you are
    trying to convert to an enum entry is valid, you can do this by calling:

    if(!Enum.IsDefi ned(typeof(MyNa meEnum), intNumber))
    {
    //throw exception
    }
    else
    {
    //keep going with your processing
    }

    Use this call sparingly though, I believe it is quite an expensive call to
    perform.


    Hope that helps
    Mark R Dawson

    "slamb@cognex.c om" wrote:
    [color=blue]
    > Hi all,
    > Does anyone know of a way to deserialize xml data that has an element
    > that represents an enum value but is actually an int?
    >
    > I know I can use [XmlEnum(Name="2 3")] to tag enum values, that works
    > but is too messy. I have many enums and tagging all values would not
    > be good.
    >
    >
    > xml data
    > <Top>
    > <Element>23</Element>
    > </Top>
    >
    > c# code
    > public enum SomeEnumType
    > {
    > Value1 = 23,
    > Value2 = 15
    > }
    >
    > public class Top
    > {
    > SomeEnumType Element;
    > }
    >
    > SomeEnumType Deserialize(Str eam stream)
    > {
    > XmlSerializer xmlSerializer = new XmlSerializer(t ypeof(Top));
    > object obj = xmlSerializer.D eserialize(stre am);
    > Top top = obj as Top;
    > if (top == null)
    > throw new SomeException() ;
    >
    > return top.Element;
    > }
    >
    >[/color]

    Comment

    • slamb@cognex.com

      #3
      Re: deserialize enum value as int

      That works for converting a single int to an enum value. However, I
      really would like the serialization process to take care of this detail
      via a hook or an attribute or something else. I have many different
      sets of data that include different enum types. Writing code to
      convert individual variables is not a clean solution.

      The problem is that the call to Deserialize will throw an exception
      when it sees 23 instead of Value1, or in the example Mark posted, 3
      instead of George.

      Thanks though.

      Comment

      • slamb@cognex.com

        #4
        Re: deserialize enum value as int

        That works for converting a single int to an enum value. However, I
        really would like the serialization process to take care of this detail
        via a hook or an attribute or something else. I have many different
        sets of data that include different enum types. Writing code to
        convert individual variables is not a clean solution.

        The problem is that the call to Deserialize will throw an exception
        when it sees 23 instead of Value1, or in the example Mark posted, 3
        instead of George.

        Thanks though.

        Comment

        Working...