Export (0) Print
Expand All

XmlEnumAttribute Class

Controls how the XmlSerializer serializes an enumeration member.

For a list of all members of this type, see XmlEnumAttribute Members.

System.Object
   System.Attribute
      System.Xml.Serialization.XmlEnumAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Field)>
Public Class XmlEnumAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Field)]
public class XmlEnumAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Field)]
public __gc class XmlEnumAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Field)
class XmlEnumAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The XmlEnumAttribute belongs to a family of attributes that controls how the XmlSerializer serializes, or deserializes, an object. For a complete list of similar attributes, see Attributes That Control XML Serialization.

Use the XmlEnumAttribute to change the enumeration that the XmlSerializer generates or recognizes (when it serializes or deserializes a class, respectively). For example, if an enumeration contains a member named One, but you prefer that the XML output be named Single, apply the XmlEnumAttribute to the enumeration member and set the Name property to "Single".

You can override the Name property value of an XmlEnumAttribute by creating an instance of the XmlEnumAttribute class, and assigning it to the XmlEnum property of an XmlAttributes object. For details, see the XmlAttributeOverrides class.

Note   In your code you can use the word XmlEnum instead of the longer XmlEnumAttribute.

Example

[Visual Basic, C#, C++] The following example applies the XmlEnumAttribute to the members of an enumeration. When the XmlSerializer generates XML data for this enumeration, the data will conform to the values of the Name properties.

[Visual Basic] 
Public Enum EmployeeStatus
   <XmlEnum("Single")> One = 1
   <XmlEnum("Double")> Two = 2
   <XmlEnum("Triple")> Three = 3
End Enum

[C#] 
public enum EmployeeStatus
{
   [XmlEnum(Name = "Single")]
   One,
   [XmlEnum(Name = "Double")]
   Two,
   [XmlEnum(Name = "Triple")]
   Three
}
   

[C++] 
public __value enum EmployeeStatus
{
   [XmlEnum(Name = S"Single")]
   One,
   [XmlEnum(Name = S"Double")]
   Two,
   [XmlEnum(Name = S"Triple")]
   Three
};
   
[Visual Basic, C#, C++] Note   In your code, you can use the word XmlEnum instead of the longer XmlEnumAttribute.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Xml.Serialization

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: System.Xml (in System.Xml.dll)

See Also

XmlEnumAttribute Members | System.Xml.Serialization Namespace | Serialize | Deserialize | XmlSerializer | XmlEnum | XmlAttributeOverrides | XmlAttributes | Introducing XML Serialization | Overriding XML Serialization | XmlAttributes | Controlling XML Serialization Using Attributes | Examples of XML Serialization

Show:
© 2014 Microsoft