次の方法で共有


XmlAttributes.XmlEnum プロパティ

XmlSerializer が列挙体メンバをシリアル化する方法を指定するオブジェクトを取得または指定します。

Public Property XmlEnum As XmlEnumAttribute
[C#]
public XmlEnumAttribute XmlEnum {get; set;}
[C++]
public: __property XmlEnumAttribute* get_XmlEnum();public: __property void set_XmlEnum(XmlEnumAttribute*);
[JScript]
public function get XmlEnum() : XmlEnumAttribute;public function set XmlEnum(XmlEnumAttribute);

プロパティ値

XmlSerializer が列挙体メンバをシリアル化する方法を指定する XmlEnumAttribute

解説

オーバーライドする各識別子について、 XmlAttributes オブジェクトを作成し、 XmlEnum プロパティを、識別子をオーバーライドする XmlEnumAttribute に設定する必要があります。列挙体を保持しているクラスの Type とオーバーライドされたメンバ名の両方を指定して、 XmlAttributes オブジェクトを XmlAttributeOverrides オブジェクトに追加します。

使用例

[Visual Basic, C#, C++] Food と FoodType という名前の 2 つのクラスをシリアル化する例を次に示します。FoodType クラスにはオーバーライドされた 2 つの列挙体が含まれています。この例では、それぞれの列挙体について XmlEnumAttribute オブジェクトが作成され、 XmlAttributes オブジェクトの XmlEnum プロパティに代入されます。この例では、次に XmlAttributes オブジェクトを XmlAttributeOverrides オブジェクトに追加し、これを使用して 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


[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 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);
   }
}
   

[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

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

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

// Return an XmlSerializer used for overriding. 
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 = S"Cold";
   xAttrs->XmlEnum = xEnum;
   xOver->Add(__typeof(FoodType), S"Low", xAttrs);

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

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

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();
}

void DeserializeObject(String* filename)
{
   XmlSerializer* mySerializer = CreateOverrider();
   FileStream* fs = new FileStream(filename, FileMode::Open);
   Food* myFood = dynamic_cast<Food*>(mySerializer->Deserialize(fs));

   Console::WriteLine(__box(myFood->Type));
}

int main()
{
   SerializeObject(S"OverrideEnum.xml");
   DeserializeObject(S"OverrideEnum.xml");
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

XmlAttributes クラス | XmlAttributes メンバ | System.Xml.Serialization 名前空間