title: C# deserializing enums from integers
body: <p>Is it possible to deserialize an enum from an int in c#. e.g. If I have the following class:</p>
  
  <pre><code>class Employee
  {
     public string Name { get; set;}
     public int EmployeeTypeID { get; set;}
  }
  </code></pre>
  
  <p>I can easily create this from XML</p>
  
  <pre><code>   &lt;Employee&gt;
         &lt;Name&gt;Joe Bloggs&lt;/Name&gt;
         &lt;EmployeeTypeID&gt;1&lt;/EmployeeTypeID&gt;
     &lt;/Employee&gt;
  </code></pre>
  
  <p>using something like this:</p>
  
  <pre><code>Employee employee = (Employee)new XmlSerializer(typeof(Employee)).Deserialize(XmlReader);
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>public enum EmployeeTypeEnum
  {
     Admin = 1,
     Sales = 2
  }
  </code></pre>
  
  <p>so my class becomes:</p>
  
  <pre><code>class Employee
  {
     public string Name { get; set;}
     public EmployeeTypeEnum EmployeeTypeID { get; set;}
  }
  </code></pre>
  
  <p>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...</p>
  
  <p>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. </p>