Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7720609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:43:05+00:00 2026-06-01T03:43:05+00:00

Is it possible to deserialize an enum from an int in c#. e.g. If

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:43:06+00:00Added an answer on June 1, 2026 at 3:43 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to Deserialize the following piece of XML into a Dictionary<int,string> object?
I have something like the following in the header class MsgBase { public: unsigned
is it possible to deserialize enums, which have a one based index? enum Status
Is it possible to serialize or deserialize any pointer? Suppose I have a class
Is this possible? Public Class Foo public var as string public sub Load(byval filename
Is it possible to serialize and deserialize a class in C++? I've been using
Is it possible to deserialize an XML file to a class in Flex without
Using Protobuf-Net, I see that it does not seem possible to deserialize a class
Is is possible to only deserialize a limited number of items from a serialized
I have JSON response from Facebook, which I don't want to deserialize into a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.