Can I specify how an enum value is to be serialzied to XML?

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
Example:

VB.NET:
  private str as String

  [XmlElementAttribute("TheString")]
  Public Readonly MyStr as String 
    Get
      Return str
    End Get
  End Sub

The XML file will be made as:

<TheString>whatever</TheString>


I'm wondering can I apply the same thing to an Enum member:

VB.NET:
  Public Enum MyEnum
    [XmlElementAttribute("ABC")]
    ActiveBrakeControl,
    [XmlElementAttribute("PAS")]
    PowerAssistedSteering
  End Enum

THis way my code can look nice, with full names, but the XML is formed as:

<carFeature>PAS</carFeature>

from
mySerializableCar.CarFeature = PowerAssistedSteering


any ideas?
 
Yes, you can by giving each enum member an XmlEnum tag:


Enum CarFeatures
[XmlEnum("PAS")]
PowerAssistedSteering
End Enum


etc..
 
Back
Top