XmlEnumAttribute コンストラクタ ()とは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlEnumAttribute コンストラクタ ()の意味・解説 

XmlEnumAttribute コンストラクタ ()

XmlEnumAttribute クラス新しインスタンス初期化します。

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

Dim instance As New XmlEnumAttribute
public XmlEnumAttribute ()
public:
XmlEnumAttribute ()
public XmlEnumAttribute ()
public function XmlEnumAttribute ()
解説解説

XmlEnumAttribute使用して既存列挙体をオーバーライドできます

メモメモ

コードでは、XmlEnumAttribute代わりに XmlEnum という短い語使用できます

使用例使用例

FoodFoodType という名前の 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 XmlOverrides 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

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

#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 XmlOverrides 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" );
}
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 XmlOverrides and XmlAttributes objects.
        XmlAttributeOverrides xOver = new XmlAttributeOverrides();
        XmlAttributes xAttrs = new XmlAttributes();

        // Add an XmlEnumAttribute for the FoodType.Low.
        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.
        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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlEnumAttribute クラス
XmlEnumAttribute メンバ
System.Xml.Serialization 名前空間
Serialize
Deserialize
XmlSerializer

XmlEnumAttribute コンストラクタ (String)

XmlSerializer が生成する (列挙体をシリアル化する場合) または認識する (列挙体を逆シリアル化する場合) XML 値を指定して、XmlEnumAttribute クラス新しインスタンス初期化します。

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

public XmlEnumAttribute (
    string name
)
public:
XmlEnumAttribute (
    String^ name
)
public XmlEnumAttribute (
    String name
)
public function XmlEnumAttribute (
    name : String
)

パラメータ

name

オーバーライドする側の列挙メンバの名前。

解説解説
メモメモ

コードでは、XmlEnumAttribute代わりに XmlEnum という短い語使用できます

使用例使用例

XmlEnumAttribute列挙体のメンバ適用する例を次に示しますXmlSerializer がこの列挙体に対応する XML データ生成する場合は、Name プロパティの値に適合するようにデータ生成します

Public Enum EmployeeStatus
   <XmlEnumAttribute("Single")> One
   <XmlEnumAttribute("Double")> Two
   <XmlEnumAttribute("Triple")> Three
End Enum
public enum EmployeeStatus
{
   [XmlEnum("Single")]
   One,
   [XmlEnum("Double")]
   Two,
   [XmlEnum("Triple")]
   Three
}
   
public enum class EmployeeStatus
{
   [XmlEnum("Single")]
   One,
   [XmlEnum("Double")]
   Two,
   [XmlEnum("Triple")]
   Three
};

public class EmployeeStatus
{
    /** @attribute XmlEnum("Single")
     */
    public static int one;
    /** @attribute XmlEnum("Double")
     */
    public static int two;
    /** @attribute XmlEnum("Triple")
     */
    public static int three;
} //EmployeeStatus
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlEnumAttribute クラス
XmlEnumAttribute メンバ
System.Xml.Serialization 名前空間
Serialize
Deserialize
XmlSerializer

XmlEnumAttribute コンストラクタ

XmlEnumAttribute クラス新しインスタンス初期化します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
XmlEnumAttribute () XmlEnumAttribute クラス新しインスタンス初期化します。

.NET Compact Framework によってサポートされています。

XmlEnumAttribute (String) XmlSerializer が生成する (列挙体をシリアル化する場合) または認識する (列挙体を逆シリアル化する場合) XML 値を指定してXmlEnumAttribute クラス新しインスタンス初期化します。

.NET Compact Framework によってサポートされています。

参照参照

関連項目

XmlEnumAttribute クラス
XmlEnumAttribute メンバ
System.Xml.Serialization 名前空間
Serialize
Deserialize
XmlSerializer



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「XmlEnumAttribute コンストラクタ ()」の関連用語

XmlEnumAttribute コンストラクタ ()のお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XmlEnumAttribute コンストラクタ ()のページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS