Export (0) Print
Expand All

XmlAttributes.XmlEnum Property

Gets or sets an object that specifies how the XmlSerializer serializes an enumeration member.

Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)

'Declaration
Public Property XmlEnum As XmlEnumAttribute
'Usage
Dim instance As XmlAttributes
Dim value As XmlEnumAttribute

value = instance.XmlEnum

instance.XmlEnum = value
/** @property */
public XmlEnumAttribute get_XmlEnum ()

/** @property */
public void set_XmlEnum (XmlEnumAttribute value)

public function get XmlEnum () : XmlEnumAttribute

public function set XmlEnum (value : XmlEnumAttribute)

Property Value

An XmlEnumAttribute that specifies how the XmlSerializer serializes an enumeration member.

For each identifier you want to override, you must create an XmlAttributes object, and set the XmlEnum property to an XmlEnumAttribute that overrides the identifier. Add the XmlAttributes object to the XmlAttributeOverrides object and specify both the Type of the class that contains the enumeration, and the overridden member name.

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 it assigns 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.

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


' This is the class that will be serialized.
Public Class Food
    Public Type As FoodType
End Class

Public Enum FoodType
    ' Subsequent code overrides these enumerations.
    Low
    High
End Enum


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("OverrideEnum.xml")
        test.DeserializeObject("OverrideEnum.xml")
    End Sub
    
    
    ' Return an XmlSerializer used for overriding. 
    Public Function CreateOverrider() As XmlSerializer
        ' Create the XmlAttributeOverrides and XmlAttributes objects.
        Dim xOver As New XmlAttributeOverrides()
        Dim xAttrs As New XmlAttributes()
        
        ' Add an XmlEnumAttribute for the FoodType.Low enumeration.
        Dim xEnum As New XmlEnumAttribute()
        xEnum.Name = "Cold"
        xAttrs.XmlEnum = xEnum
        xOver.Add(GetType(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(GetType(FoodType), "High", xAttrs)
        
        ' Create the XmlSerializer, and return it.
        Return New XmlSerializer(GetType(Food), xOver)
    End Function
    
        
    Public Sub SerializeObject(ByVal filename As String)
        ' Create an instance of the XmlSerializer class.
        Dim mySerializer As XmlSerializer = CreateOverrider()
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create an instance of the class that will be serialized.
        Dim myFood As New Food()
        
        ' Set the object properties.
        myFood.Type = FoodType.High
        
        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myFood)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(ByVal filename As String)
        Dim mySerializer As XmlSerializer = CreateOverrider()
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myFood As Food = CType(mySerializer.Deserialize(fs), Food)
        
        Console.WriteLine(myFood.Type)
    End Sub
End Class


import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

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

    Food()
    {
        type = new FoodType();
    } //Food
} //Food

public class FoodType
{
    public int member;

    FoodType()
    {
        member = 0;
    } //FoodType

    public int get_member()
    {
        return member;
    } //get_member

    public void set_member(int n)
    {
        member = n;
    } //set_member

    public String getMemberType()
    {
        if (member == 0) {
            return "low";
        }
        else {
            return "high";
        }
    } //getMemberType

    public static int low = 0;
    public static int high = 1;
} //FoodType

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeObject("OverrideEnum.xml");
        test.DeserializeObject("OverrideEnum.xml");
    } //main

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

        // Add an XmlEnumAttribute for the FoodType.Low enumeration.
        XmlEnumAttribute xEnum = new XmlEnumAttribute();
        xEnum.set_Name("Cold");
        xAttrs.set_XmlEnum(xEnum);
        xOver.Add(FoodType.class.ToType(), "low", xAttrs);

        // Add an XmlEnumAttribute for the FoodType.High enumeration.
        xAttrs = new XmlAttributes();
        xEnum = new XmlEnumAttribute();
        xEnum.set_Name("Hot");
        xAttrs.set_XmlEnum(xEnum);
        xOver.Add(FoodType.class.ToType(), "high", xAttrs);

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

    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.set_member(FoodType.high);

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

    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.getMemberType());
    } //DeserializeObject
} //Run

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft