次の方法で共有


XmlSerializer コンストラクタ (Type)

指定した型のオブジェクトを XML ドキュメントにシリアル化したり、XML ドキュメントを指定した型のオブジェクトに逆シリアル化したりできる、XmlSerializer クラスの新しいインスタンスを初期化します。

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

構文

'宣言
Public Sub New ( _
    type As Type _
)
'使用
Dim type As Type

Dim instance As New XmlSerializer(type)
public XmlSerializer (
    Type type
)
public:
XmlSerializer (
    Type^ type
)
public XmlSerializer (
    Type type
)
public function XmlSerializer (
    type : Type
)

パラメータ

  • type
    XmlSerializer がシリアル化できるオブジェクトの型。

解説

通常、アプリケーションでは、XmlSerializer が 1 つの XML インスタンス ドキュメントへと変換するクラスが複数定義されています。ただし、XmlSerializer が認識する必要がある型は XML ルート要素を表すクラスの型だけです。XmlSerializer は、自動的にすべての下位クラス インスタンスをシリアル化します。同様に、逆シリアル化に必要なのは XML ルート要素の型だけです。

使用例

Widget という名前のシンプルなオブジェクトをシリアル化する XmlSerializer の構築例を次に示します。この例では、オブジェクトの各種プロパティを設定してから Serialize メソッドを呼び出します。

Private Sub SerializeObject(ByVal filename As String)
    Dim serializer As New XmlSerializer(GetType(OrderedItem))
    
    ' Create an instance of the class to be serialized.
    Dim i As New OrderedItem()
    
    ' Set the public property values.
    With i
        .ItemName = "Widget"
        .Description = "Regular Widget"
        .Quantity = 10
        .UnitPrice = CDec(2.3)
    End With
    
    ' Writing the document requires a TextWriter.
    Dim writer As New StreamWriter(filename)
    
    ' Serialize the object, and close the TextWriter.
    serializer.Serialize(writer, i)
    writer.Close()
End Sub


' This is the class that will be serialized.
Public Class OrderedItem
    Public ItemName As String
    Public Description As String
    Public UnitPrice As Decimal
    Public Quantity As Integer
End Class
private void SerializeObject(string filename)
{
   XmlSerializer serializer = 
   new XmlSerializer(typeof(OrderedItem));

   // Create an instance of the class to be serialized.
   OrderedItem i = new OrderedItem();

   // Set the public property values.
   i.ItemName = "Widget";
   i.Description = "Regular Widget";
   i.Quantity = 10;
   i.UnitPrice = (decimal) 2.30;

   // Writing the document requires a TextWriter.
   TextWriter writer = new StreamWriter(filename);

   // Serialize the object, and close the TextWriter.
   serializer.Serialize(writer, i);
   writer.Close();
}

// This is the class that will be serialized.
public class OrderedItem
{
   public string ItemName;
   public string Description;
   public decimal UnitPrice;
   public int Quantity;
}
private:
   void SerializeObject( String^ filename )
   {
      XmlSerializer^ serializer =
         gcnew XmlSerializer( OrderedItem::typeid );

      // Create an instance of the class to be serialized.
      OrderedItem^ i = gcnew OrderedItem;

      // Set the public property values.
      i->ItemName = "Widget";
      i->Description = "Regular Widget";
      i->Quantity = 10;
      i->UnitPrice = (Decimal)2.30;

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

      // Serialize the object, and close the TextWriter.
      serializer->Serialize( writer, i );
      writer->Close();
   }

public:
   // This is the class that will be serialized.
   ref class OrderedItem
   {
   public:
      String^ ItemName;
      String^ Description;
      Decimal UnitPrice;
      int Quantity;
   };
private void SerializeObject(String filename)
{
    XmlSerializer serializer =
        new XmlSerializer(OrderedItem.class.ToType());

    // Create an instance of the class to be serialized.
    OrderedItem i = new OrderedItem();

    // Set the public property values.
    i.itemName = "Widget";
    i.description = "Regular Widget";
    i.quantity = 10;
    i.unitPrice = Convert.ToDecimal(2.3);

    // Writing the document requires a TextWriter.
    TextWriter writer = new StreamWriter(filename);

    // Serialize the object, and close the TextWriter.
    serializer.Serialize(writer, i);
    writer.Close();
} //SerializeObject

// This is the class that will be serialized.
public class OrderedItem
{
    public String itemName;
    public String description;
    public System.Decimal unitPrice;
    public int quantity;
} //OrderedItem

プラットフォーム

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

開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。

バージョン情報

.NET Framework

サポート対象 : 2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 2.0

参照

関連項目

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

その他の技術情報

XML シリアル化の概要
方法 : XML ストリームの代替要素名を指定する
属性を使用した XML シリアル化の制御
XML シリアル化の例
XML スキーマ定義ツール (Xsd.exe)