XmlEnumAttribute Constructors

Definition

Initializes a new instance of the XmlEnumAttribute class.

Overloads

XmlEnumAttribute()

Initializes a new instance of the XmlEnumAttribute class.

XmlEnumAttribute(String)

Initializes a new instance of the XmlEnumAttribute class, and specifies the XML value that the XmlSerializer generates or recognizes (when it serializes or deserializes the enumeration, respectively).

XmlEnumAttribute()

Source:
XmlEnumAttribute.cs
Source:
XmlEnumAttribute.cs
Source:
XmlEnumAttribute.cs

Initializes a new instance of the XmlEnumAttribute class.

C#
public XmlEnumAttribute();

Examples

The following example serializes two classes named Food and FoodType. The FoodType class contains two enumerations that are overridden and, for each enumeration, the example creates an XmlEnumAttribute object that is assigned to the XmlEnum property of an XmlAttributes object. The example then adds the XmlAttributes object to an XmlAttributeOverrides object, which is used to create an XmlSerializer.

C#
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Food
{
   public FoodType Type;
}

public enum FoodType
{
   // Subsequent code overrides these enumerations.
   Low,
   High
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("OverrideEnum.xml");
      test.DeserializeObject("OverrideEnum.xml");
   }

   // Return an XmlSerializer used for overriding.
   public XmlSerializer CreateOverrider()
   {
      // Create the XmlOverrides and XmlAttributes objects.
      XmlAttributeOverrides xOver = new XmlAttributeOverrides();
      XmlAttributes xAttrs = new XmlAttributes();

      // Add an XmlEnumAttribute for the FoodType.Low enumeration.
      XmlEnumAttribute xEnum = new XmlEnumAttribute();
      xEnum.Name = "Cold";
      xAttrs.XmlEnum = xEnum;
      xOver.Add(typeof(FoodType), "Low", xAttrs);

      // Add an XmlEnumAttribute for the FoodType.High enumeration.
      xAttrs = new XmlAttributes();
      xEnum = new XmlEnumAttribute();
      xEnum.Name = "Hot";
      xAttrs.XmlEnum = xEnum;
      xOver.Add(typeof(FoodType), "High", xAttrs);

      // Create the XmlSerializer, and return it.
      return new XmlSerializer(typeof(Food), xOver);
   }

   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =  CreateOverrider();
      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Food myFood = new Food();

      // Set the object properties.
      myFood.Type = FoodType.High;

      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myFood);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlSerializer mySerializer = CreateOverrider();
      FileStream fs = new FileStream(filename, FileMode.Open);
      Food myFood = (Food)
      mySerializer.Deserialize(fs);

      Console.WriteLine(myFood.Type);
   }
}

Remarks

You can use the XmlEnumAttribute to override an existing enumeration.

Note

You can use the word XmlEnum in your code instead of the longer XmlEnumAttribute.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

XmlEnumAttribute(String)

Source:
XmlEnumAttribute.cs
Source:
XmlEnumAttribute.cs
Source:
XmlEnumAttribute.cs

Initializes a new instance of the XmlEnumAttribute class, and specifies the XML value that the XmlSerializer generates or recognizes (when it serializes or deserializes the enumeration, respectively).

C#
public XmlEnumAttribute(string name);
C#
public XmlEnumAttribute(string? name);

Parameters

name
String

The overriding name of the enumeration member.

Examples

The following example applies the XmlEnumAttribute to the members of an enumeration. When the XmlSerializer generates XML data for this enumeration, the data conforms to the values of the Name properties.

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

Remarks

Note

You can use the word XmlEnum in your code instead of the longer XmlEnumAttribute.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0