Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# deserializing enums from integers

Is it possible to deserialize an enum from an int in c#. e.g. If I have the following class:

class Employee
{
   public string Name { get; set;}
   public int EmployeeTypeID { get; set;}
}

I can easily create this from XML

   <Employee>
       <Name>Joe Bloggs</Name>
       <EmployeeTypeID>1</EmployeeTypeID>
   </Employee>

using something like this:

Employee employee = (Employee)new XmlSerializer(typeof(Employee)).Deserialize(XmlReader);

With very little work involved, this allows me to use one generic service that I can use for all database objects by feeding a select command, connection string and a type in to and retrieve an array of objects without any need for further mapping. However I have come unstuck with enums. Supposing now instead of being an integer EmployeeType is an enum:

public enum EmployeeTypeEnum
{
   Admin = 1,
   Sales = 2
}

so my class becomes:

class Employee
{
   public string Name { get; set;}
   public EmployeeTypeEnum EmployeeTypeID { get; set;}
}

Can I use the same XML and make c# recognise that the int value of EmployeeTypeID in the xml should correspond with the int value of the enum? There are other questions similar out there, but none have a very satisfactory answer are quite old, and involve wholesale changes to code. I am hoping for a better solution...

As a possible separate note (and slightly in anticipation of some responses), is using enums for this a practise best avoided? Should I be using Key-Value pairs? I would always use Key-value pairs (or similar) if there were likely to be changes, but in this case EmployeeType is fixed and will never change.

like image 590
GarethD Avatar asked Mar 30 '12 13:03

GarethD


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

Theoretically (= I haven't tried it), adding the XmlEnum attribute to your enum values should do the trick:

public enum EmployeeTypeEnum 
{ 
    [XmlEnum("1")] Admin = 1, 
    [XmlEnum("2")] Sales = 2 
} 

This tells XmlSerializer that a value of EmployeeTypeEnum.Admin is to be serialized as the string 1 and vice-versa (which is what you need).

Regarding your side note: I don't see using enums here as a problem. If the values in the database are integers and have a fixed meaning, enums are a good solution and, in addition, serve as a documentation to the database values.

like image 50
Heinzi Avatar answered Sep 28 '22 05:09

Heinzi


If you xml is in this format:

<Employee>
  <Name>Shiv</Name>
  <EmployeeTypeID>Sales</EmployeeTypeID>
</Employee>

It will work as is. If you decorate your enum with the XmlEnum attribute like so:

    public enum EmployeeTypeEnum
{
    [XmlEnum("1")]
    Admin = 1,
    [XmlEnum("2")]
    Sales = 2
}

Then you can use integer values in your xml file and they will be mapped to the enum automagically.

Personally, I prefer using enums in cases like this. Even if the items in the enum could increase over time, I prefer using enums. In fact such enums are code generated from the database, so in code you don't work with Ids (much more readable).

like image 29
Shiv Kumar Avatar answered Sep 28 '22 07:09

Shiv Kumar