XmlAttributes.XmlEnum 屬性

定義

取得或設定物件,指定 XmlSerializer 如何序列化列舉型別 (Enumeration) 成員。

public:
 property System::Xml::Serialization::XmlEnumAttribute ^ XmlEnum { System::Xml::Serialization::XmlEnumAttribute ^ get(); void set(System::Xml::Serialization::XmlEnumAttribute ^ value); };
public System.Xml.Serialization.XmlEnumAttribute XmlEnum { get; set; }
public System.Xml.Serialization.XmlEnumAttribute? XmlEnum { get; set; }
member this.XmlEnum : System.Xml.Serialization.XmlEnumAttribute with get, set
Public Property XmlEnum As XmlEnumAttribute

屬性值

XmlEnumAttribute

XmlEnumAttribute,指定 XmlSerializer 如何序列化列舉型別成員。

範例

下列範例會序列化名為 和 FoodType 的兩個 Food 類別。 類別 FoodType 包含兩個已覆寫的列舉,而且針對每個列舉,此範例會 XmlEnumAttribute 建立物件,該物件會指派給 XmlEnum 物件的 屬性 XmlAttributes 。 此範例接著會將 XmlAttributes 物件新增至 XmlAttributeOverrides 物件,以用來建立 XmlSerializer

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public enum class FoodType
{
   // Subsequent code overrides these enumerations.
   Low, High
};

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

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

   // Add an XmlEnumAttribute for the FoodType.Low enumeration.
   XmlEnumAttribute^ xEnum = gcnew XmlEnumAttribute;
   xEnum->Name = "Cold";
   xAttrs->XmlEnum = xEnum;
   xOver->Add( FoodType::typeid, "Low", xAttrs );

   // Add an XmlEnumAttribute for the FoodType.High enumeration.
   xAttrs = gcnew XmlAttributes;
   xEnum = gcnew XmlEnumAttribute;
   xEnum->Name = "Hot";
   xAttrs->XmlEnum = xEnum;
   xOver->Add( FoodType::typeid, "High", xAttrs );

   // Create the XmlSerializer, and return it.
   return gcnew XmlSerializer( Food::typeid,xOver );
}

void SerializeObject( String^ filename )
{
   // Create an instance of the XmlSerializer class.
   XmlSerializer^ mySerializer = CreateOverrider();

   // Writing the file requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Create an instance of the class that will be serialized.
   Food^ myFood = gcnew Food;

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

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

void DeserializeObject( String^ filename )
{
   XmlSerializer^ mySerializer = CreateOverrider();
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Food^ myFood = dynamic_cast<Food^>(mySerializer->Deserialize( fs ));
   Console::WriteLine( myFood->Type );
}

int main()
{
   SerializeObject( "OverrideEnum.xml" );
   DeserializeObject( "OverrideEnum.xml" );
}
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 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.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);
   }
}
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

備註

針對您想要覆寫的每個識別碼,您必須建立 XmlAttributes 物件,並將 屬性設定 XmlEnumXmlEnumAttribute 覆寫識別碼的 。 將 XmlAttributes 物件新增至 XmlAttributeOverrides 物件,並指定 Type 包含 列舉的 類別和覆寫的成員名稱。

適用於