Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
I have a set of C# classes produced by XSD.exe
I am having difficulty geting the value from this enum:

C#
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.1432")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://emcs.dgtaxud.ec/v10/tcl")]
public enum OriginTypeCode
{
    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("1")]
    Item1,
    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("2")]
    Item2,
}


My code is written in C++/CLI.

All I can extract is "Item1" when I want "1"
Posted
Comments
Sergey Alexandrovich Kryukov 5-Mar-11 17:59pm    
It's wrong but makes a pretty interesting Question (I voted 4). I enjoyed to answer :-)
Thank you.
--SA

1 solution

Probably you're messing up the names of enumeration members with their underlying integer values. Look at the sample of usage if this attribute here: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlenumattribute.aspx[^].

The serialization mechanism for enumeration is based of enumeration member identifiers, not underlying integer values. The values or the XmlEnum attribute should be valid and unique member identifiers, but "1", "2" are not. To assign integer values you need you should do something like this:

C#
public enum OriginTypeCode
{
    Item1 = 1,
    Item2 = 2,
}


Also, there is no boundary between C++/CLI and C#. If you define any enumeration type in an assembly written in any of these languages and reference this assembly in another assembly written in any of these language, the declarations will be interpreted in exact same way, including those underlying integer values.

When you use serialized presentation of your data, the file/stream written using by one of the languages, will be correctly read by the assembly written in a different language, because they share the same enumeration type declaration and because the enumeration members are identified by their names. This is an feature important for compatibility between versions: if you add/insert additional enumeration member in your enumeration types, existing members from your legacy data will be interpreted by new version of your sofrware semantically, which is usually what you want.

The mapping between enumeration members and their respective underlying integer values is actually a very delicate matter and a source of some subtle problems (but features, too). To understand this, please read my article on the topic: Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^]. I explain it in detail.

There is no need to use System.Xml.Serialization.XmlEnumAttribute in your case. (By the way, recommended practice is using abbreviated name at the point of attribute application: [XmlEnum] instead of [XmlEnumAttribute].) One of the uses of this attribute it to give enumeration members more descriptive names when it it not possible to do it in the source code; another use is using serialization for integration with a legacy system which cannot be modified.

—SA
 
Share this answer
 
v4
Comments
Espen Harlinn 5-Mar-11 17:53pm    
Fair enought, my 5
Sergey Alexandrovich Kryukov 5-Mar-11 17:58pm    
Thank you, this is pretty interesting question, even though based on OP misconception. Perhaps, I'll up-vote it...
--SA
Ger Hayden 5-Mar-11 18:01pm    
Thanks for a superbly detailed answer - It will take me a while to apply it. Its late at the end of a long week. I had hoped that the XSD.exe would have kicked out accurate code...
Sergey Alexandrovich Kryukov 5-Mar-11 19:17pm    
You're welcome.
Will you formally accept my Answer? I think it will help you.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900