473,394 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Enum Serialization as Integer or Literal

I have an Enum like this:

public enum DAYS
{
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 7
}
By default enumerations are de-/serilized (in XML) as literals, e.g.
<Tuesday>.

I would like to be able to de-/serialize the Enum as "literal"
(<Tuesday>) and as "integer" (<2>) depending on the user (runtime).

My first idea was XmlEnum, but it does not work runtime :-(

???

Thanks

Sep 7 '06 #1
3 7636
Int is no problem.

DAYS day = (DAYS) 1; // that's monday

Strings are more iffy. You'll have to create a method that loops through all
of the enums until it finds a string match. Then you'll have to update it
every time you add something to the enum (groo).
It "might" be possible to get all the enums through reflection, although
i've never tried reflection on a primitive type like an enum, but you can
look it up to see if it is supported.

HTH

Simon

"Diego" <di*************@hotmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
I have an Enum like this:

public enum DAYS
{
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 7
}
By default enumerations are de-/serilized (in XML) as literals, e.g.
<Tuesday>.

I would like to be able to de-/serialize the Enum as "literal"
(<Tuesday>) and as "integer" (<2>) depending on the user (runtime).

My first idea was XmlEnum, but it does not work runtime :-(

???

Thanks

Sep 7 '06 #2
"Simon Tamman" <i_**********************************@NOSPAMhotmai l.com>
wrote in news:Pz******************@newsfe5-win.ntli.net:
DAYS day = (DAYS) 1; // that's monday

Strings are more iffy. You'll have to create a method that loops
through all of the enums until it finds a string match. Then you'll
have to update it every time you add something to the enum (groo).
It "might" be possible to get all the enums through reflection,
although i've never tried reflection on a primitive type like an enum,
but you can look it up to see if it is supported.
If you are trying to convert a string to the equivalent Enum, look at
Enum.Parse. It does exactly that. No need to iterate over the Enum to
find a match.

-mdb

Sep 7 '06 #3
Awwww noooo way. I could have saved myself mucho time had I known that
earlier (although these days I usually avoid enums in favour of the state
pattern).
Thanks for the info!

"Michael Bray" <mbray@makeDIntoDot_ctiusaDcomwrote in message
news:Xn****************************@207.46.248.16. ..
"Simon Tamman" <i_**********************************@NOSPAMhotmai l.com>
wrote in news:Pz******************@newsfe5-win.ntli.net:
DAYS day = (DAYS) 1; // that's monday

Strings are more iffy. You'll have to create a method that loops
through all of the enums until it finds a string match. Then you'll
have to update it every time you add something to the enum (groo).
It "might" be possible to get all the enums through reflection,
although i've never tried reflection on a primitive type like an enum,
but you can look it up to see if it is supported.

If you are trying to convert a string to the equivalent Enum, look at
Enum.Parse. It does exactly that. No need to iterate over the Enum to
find a match.

-mdb

Sep 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Voronkov Konstantin | last post by:
Thank you for answer, but I still did not got *how* to make serialization of enum type. Can you provide more instructions or hint, please? My task is to serialize enum to something like byte...
0
by: psy000 | last post by:
Hi, I have a C# web service client that talks to a JAVA application sever. I use AXIS to generate the WSDL file, use wsdl.exe to generate proxy stub c# code. When I try to use c# client connect...
7
by: d.kaufmann | last post by:
i have this part of code: typedef enum { OW_SEARCH_ROM = 0xF0, OW_MATCH_ROM = 0x55, OW_CONDITIONAL_SEARCH_ROM = 0xEC, OW_HANDLE_SELECT = 0x0F } ow_command_t;
1
by: Chris Dunaway | last post by:
Suppose I have the following enum: public enum MyEnum { EnumVal1 = 1, EnumVal2 = 2, EnumVal3 = 3 } I want to serialize the enum but I want the /names/ of the enum to be stored in the .xml...
1
by: Powers | last post by:
I currently have a large number of enums implemented into my Web Service. I am reworking the XML serialization of these enums. At present, each enum has hardcoded values, for example: xAttrs...
6
by: bsma1 | last post by:
I building a web service that has an enum I want the consuming application to be able to use. I have the enum declared in the web service as: public enum myEnum { ONE = 1, TWO = 2, };
2
by: wasco77 | last post by:
Hi all, I've a problem using enum tag. I need to create a structure like this: // --------------------------------------- public enum typeSupply { 5VOnly,
2
by: Daniel Jeffrey | last post by:
Hello. Can anyone help me please. I have created an Enumeration, and I loop through it, it all works ok, except the first item returns 2 times. Code is below. When called I Get "Text Edit" 2...
11
by: =?Utf-8?B?dG9iaXdhbl9rZW5vYmk=?= | last post by:
The following code is in a custom deserializer: object value = (int) 1; string nameToParse = Enum.GetName(field.FieldType, value); value = Enum.Parse(field.FieldType, nameToParse); Currently...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.