XmlSerializer コンストラクタ (Type, Type )
指定した型のオブジェクトを XML ドキュメントにシリアル化したり、XML ドキュメントを指定した型のオブジェクトに逆シリアル化したりできる、XmlSerializer クラスの新しいインスタンスを初期化します。プロパティまたはフィールドが配列を返す場合、extraTypes パラメータには、その配列に挿入できるオブジェクトを指定します。
名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文
'宣言
Public Sub New ( _
type As Type, _
extraTypes As Type() _
)
'使用
Dim type As Type
Dim extraTypes As Type()
Dim instance As New XmlSerializer(type, extraTypes)
public XmlSerializer (
Type type,
Type[] extraTypes
)
public:
XmlSerializer (
Type^ type,
array<Type^>^ extraTypes
)
public XmlSerializer (
Type type,
Type[] extraTypes
)
public function XmlSerializer (
type : Type,
extraTypes : Type[]
)
パラメータ
- type
XmlSerializer がシリアル化できるオブジェクトの型。
- extraTypes
シリアル化する追加のオブジェクト型の Type 配列。
解説
既定では、パブリック プロパティまたはパブリック フィールドがオブジェクトまたはオブジェクトの配列を返したときには、それらのオブジェクトの型が自動的にシリアル化されます。ただし、Object 型の配列を返すフィールドまたはプロパティがクラスに含まれている場合は、すべてのオブジェクトが配列に挿入されます。この場合、XmlSerializer に対して、Object 配列に挿入される可能性のあるすべてのオブジェクトの型を通知しておく必要があります。この場合は、extraTypes パラメータを使用して、シリアル化または逆シリアル化する追加のオブジェクト型を指定する必要があります。
extraTypes パラメータを使用して、基本クラスから派生する型を指定することもできます。たとえば、Phone という名前の基本クラスがあり、このクラスから InternationalPhone という名前のクラスが派生するとします。extraTypes パラメータを使用して、この派生型を指定することもできます。
使用例
オブジェクトの配列を返すパブリック フィールドを含むクラスのインスタンスをシリアル化する例を次に示します。XmlSerializer コンストラクタの extraTypes パラメータは、シリアル化して配列に挿入できるオブジェクトの型を指定します。
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
' This defines the object that will be serialized.
Public Class Teacher
Public Name As String
Public Sub New()
End Sub 'New
' Note that the Info field returns an array of objects.
' Any object can be added to the array by adding the
' object type to the array passed to the extraTypes argument.
<XmlArray(ElementName := "ExtraInfo", IsNullable := True)> _
Public Info() As Object
Public PhoneInfo As Phone
End Class 'Teacher
' This defines one of the extra types to be included.
Public Class Address
Public City As String
Public Sub New()
End Sub 'New
Public Sub New(city As String)
me.City = city
End Sub 'New
End Class 'Address
' Another extra type to include.
Public Class Phone
Public PhoneNumber As String
Public Sub New()
End Sub 'New
Public Sub New(phoneNumber As String)
me.PhoneNumber = phoneNumber
End Sub 'New
End Class 'Phone
' Another type, derived from Phone.
Public Class InternationalPhone
Inherits Phone
Public CountryCode As String
Public Sub New()
End Sub 'New
Public Sub New(countryCode As String)
me.CountryCode = countryCode
End Sub 'New
End Class 'InternationalPhone
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("Teacher.xml")
test.DeserializeObject("Teacher.xml")
End Sub 'Main
Private Sub SerializeObject(filename As String)
' Writing the file requires a TextWriter.
Dim myStreamWriter As New StreamWriter(filename)
' Create a Type array.
Dim extraTypes(2) As Type
extraTypes(0) = GetType(Address)
extraTypes(1) = GetType(Phone)
extraTypes(2) = GetType(InternationalPhone)
' Create the XmlSerializer instance.
Dim mySerializer As New XmlSerializer(GetType(Teacher), extraTypes)
Dim teacher As New Teacher()
teacher.Name = "Mike"
' Add extra types to the Teacher object.
Dim info(1) As Object
info(0) = New Address("Springville")
info(1) = New Phone("555-0100")
teacher.Info = info
teacher.PhoneInfo = New InternationalPhone("000")
mySerializer.Serialize(myStreamWriter, teacher)
myStreamWriter.Close()
End Sub 'SerializeObject
Private Sub DeserializeObject(filename As String)
' Create a Type array.
Dim extraTypes(2) As Type
extraTypes(0) = GetType(Address)
extraTypes(1) = GetType(Phone)
extraTypes(2) = GetType(InternationalPhone)
' Create the XmlSerializer instance.
Dim mySerializer As New XmlSerializer(GetType(Teacher), extraTypes)
' Reading a file requires a FileStream.
Dim fs As New FileStream(filename, FileMode.Open)
Dim teacher As Teacher = CType(mySerializer.Deserialize(fs), Teacher)
' Read the extra information.
Dim a As Address = CType(teacher.Info(0), Address)
Dim p As Phone = CType(teacher.Info(1), Phone)
Dim Ip As InternationalPhone = CType(teacher.PhoneInfo, InternationalPhone)
Console.WriteLine(teacher.Name)
Console.WriteLine(a.City)
Console.WriteLine(p.PhoneNumber)
Console.WriteLine(Ip.CountryCode)
End Sub 'DeserializeObject
End Class 'Run
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
// This defines the object that will be serialized.
public class Teacher
{
public string Name;
public Teacher(){}
/* Note that the Info field returns an array of objects.
Any object can be added to the array by adding the
object type to the array passed to the extraTypes argument. */
[XmlArray (ElementName = "ExtraInfo", IsNullable = true)]
public object[] Info;
public Phone PhoneInfo;
}
// This defines one of the extra types to be included.
public class Address
{
public string City;
public Address(){}
public Address(string city)
{
City = city;
}
}
// Another extra type to include.
public class Phone
{
public string PhoneNumber;
public Phone(){}
public Phone(string phoneNumber)
{
PhoneNumber = phoneNumber;
}
}
// Another type, derived from Phone
public class InternationalPhone:Phone
{
public string CountryCode;
public InternationalPhone(){}
public InternationalPhone(string countryCode)
{
CountryCode = countryCode;
}
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("Teacher.xml");
test.DeserializeObject("Teacher.xml");
}
private void SerializeObject(string filename)
{
// Writing the file requires a TextWriter.
TextWriter myStreamWriter = new StreamWriter(filename);
// Create a Type array.
Type [] extraTypes= new Type[3];
extraTypes[0] = typeof(Address);
extraTypes[1] = typeof(Phone);
extraTypes[2] = typeof(InternationalPhone);
// Create the XmlSerializer instance.
XmlSerializer mySerializer = new XmlSerializer
(typeof(Teacher),extraTypes);
Teacher teacher = new Teacher();
teacher.Name = "Mike";
// Add extra types to the Teacher object
object [] info = new object[2];
info[0] = new Address("Springville");
info[1] = new Phone("555-0100");
teacher.Info = info;
teacher.PhoneInfo = new InternationalPhone("000");
mySerializer.Serialize(myStreamWriter,teacher);
myStreamWriter.Close();
}
private void DeserializeObject(string filename)
{
// Create a Type array.
Type [] extraTypes= new Type[3];
extraTypes[0] = typeof(Address);
extraTypes[1] = typeof(Phone);
extraTypes[2] = typeof(InternationalPhone);
// Create the XmlSerializer instance.
XmlSerializer mySerializer = new XmlSerializer
(typeof(Teacher),extraTypes);
// Reading a file requires a FileStream.
FileStream fs = new FileStream(filename, FileMode.Open);
Teacher teacher = (Teacher) mySerializer.Deserialize(fs);
// Read the extra information.
Address a = (Address)teacher.Info[0];
Phone p = (Phone) teacher.Info[1];
InternationalPhone Ip =
(InternationalPhone) teacher.PhoneInfo;
Console.WriteLine(teacher.Name);
Console.WriteLine(a.City);
Console.WriteLine(p.PhoneNumber);
Console.WriteLine(Ip.CountryCode);
}
}
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
ref class Address;
ref class Phone;
// This defines the object that will be serialized.
public ref class Teacher
{
public:
String^ Name;
Teacher(){}
/* Note that the Info field returns an array of objects.
Any object can be added to the array by adding the
object type to the array passed to the extraTypes argument. */
[XmlArray(ElementName="ExtraInfo",IsNullable=true)]
array<Object^>^Info;
Phone^ PhoneInfo;
};
// This defines one of the extra types to be included.
public ref class Address
{
public:
String^ City;
Address(){}
Address( String^ city )
{
City = city;
}
};
// Another extra type to include.
public ref class Phone
{
public:
String^ PhoneNumber;
Phone(){}
Phone( String^ phoneNumber )
{
PhoneNumber = phoneNumber;
}
};
// Another type, derived from Phone
public ref class InternationalPhone: public Phone
{
public:
String^ CountryCode;
InternationalPhone(){}
InternationalPhone( String^ countryCode )
{
CountryCode = countryCode;
}
};
public ref class Run
{
public:
static void main()
{
Run^ test = gcnew Run;
test->SerializeObject( "Teacher.xml" );
test->DeserializeObject( "Teacher.xml" );
}
private:
void SerializeObject( String^ filename )
{
// Writing the file requires a TextWriter.
TextWriter^ myStreamWriter = gcnew StreamWriter( filename );
// Create a Type array.
array<Type^>^extraTypes = gcnew array<Type^>(3);
extraTypes[ 0 ] = Address::typeid;
extraTypes[ 1 ] = Phone::typeid;
extraTypes[ 2 ] = InternationalPhone::typeid;
// Create the XmlSerializer instance.
XmlSerializer^ mySerializer = gcnew XmlSerializer( Teacher::typeid,extraTypes );
Teacher^ teacher = gcnew Teacher;
teacher->Name = "Mike";
// Add extra types to the Teacher object
array<Object^>^info = gcnew array<Object^>(2);
info[ 0 ] = gcnew Address( "Springville" );
info[ 1 ] = gcnew Phone( "555-0100" );
teacher->Info = info;
teacher->PhoneInfo = gcnew InternationalPhone( "000" );
mySerializer->Serialize( myStreamWriter, teacher );
myStreamWriter->Close();
}
void DeserializeObject( String^ filename )
{
// Create a Type array.
array<Type^>^extraTypes = gcnew array<Type^>(3);
extraTypes[ 0 ] = Address::typeid;
extraTypes[ 1 ] = Phone::typeid;
extraTypes[ 2 ] = InternationalPhone::typeid;
// Create the XmlSerializer instance.
XmlSerializer^ mySerializer = gcnew XmlSerializer( Teacher::typeid,extraTypes );
// Reading a file requires a FileStream.
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
Teacher^ teacher = dynamic_cast<Teacher^>(mySerializer->Deserialize( fs ));
// Read the extra information.
Address^ a = dynamic_cast<Address^>(teacher->Info[ 0 ]);
Phone^ p = dynamic_cast<Phone^>(teacher->Info[ 1 ]);
InternationalPhone^ Ip = dynamic_cast<InternationalPhone^>(teacher->PhoneInfo);
Console::WriteLine( teacher->Name );
Console::WriteLine( a->City );
Console::WriteLine( p->PhoneNumber );
Console::WriteLine( Ip->CountryCode );
}
};
int main()
{
Run::main();
}
import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;
// This defines the object that will be serialized.
public class Teacher
{
public String name;
public Teacher()
{
} //Teacher
/* Note that the Info field returns an array of objects.
Any object can be added to the array by adding the
object type to the array passed to the extraTypes argument. */
/** @attribute XmlArray(ElementName = "ExtraInfo", IsNullable = true)
*/
public Object info[];
public Phone phoneInfo;
} //Teacher
// This defines one of the extra types to be included.
public class Address
{
public String city;
public Address()
{
} //Address
public Address(String tCity)
{
city = tCity;
} //Address
} //Address
// Another extra type to include.
public class Phone
{
public String phoneNumber;
public Phone()
{
} //Phone
public Phone(String tPhoneNumber)
{
phoneNumber = tPhoneNumber;
} //Phone
} //Phone
// Another type, derived from Phone
public class InternationalPhone extends Phone
{
public String countryCode;
public InternationalPhone()
{
} //InternationalPhone
public InternationalPhone(String tCountryCode)
{
countryCode = tCountryCode;
} //InternationalPhone
} //InternationalPhone
public class Run
{
public static void main(String[] args)
{
Run test = new Run();
test.SerializeObject("Teacher.xml");
test.DeserializeObject("Teacher.xml");
} //main
private void SerializeObject(String filename)
{
// Writing the file requires a TextWriter.
TextWriter myStreamWriter = new StreamWriter(filename);
// Create a Type array.
Type extraTypes[] = new Type[3];
extraTypes.set_Item(0, Address.class.ToType());
extraTypes.set_Item(1, Phone.class.ToType());
extraTypes.set_Item(2, InternationalPhone.class.ToType());
// Create the XmlSerializer instance.
XmlSerializer mySerializer =
new XmlSerializer(Teacher.class.ToType(), extraTypes);
Teacher teacher = new Teacher();
teacher.name = "Mike";
// Add extra types to the Teacher object
Object info[] = new Object[2];
info.set_Item(0, new Address("Springville"));
info.set_Item(1, new Phone("000-0000"));
teacher.info = info;
teacher.phoneInfo = new InternationalPhone("000");
mySerializer.Serialize(myStreamWriter, teacher);
myStreamWriter.Close();
} //SerializeObject
private void DeserializeObject(String filename)
{
// Create a Type array.
Type extraTypes[] = new Type[3];
extraTypes.set_Item(0, Address.class.ToType());
extraTypes.set_Item(1, Phone.class.ToType());
extraTypes.set_Item(2, InternationalPhone.class.ToType());
// Create the XmlSerializer instance.
XmlSerializer mySerializer =
new XmlSerializer(Teacher.class.ToType(), extraTypes);
// Reading a file requires a FileStream.
FileStream fs = new FileStream(filename, FileMode.Open);
Teacher teacher = (Teacher)mySerializer.Deserialize(fs);
// Read the extra information.
Address a = (Address)teacher.info[0];
Phone p = (Phone)teacher.info[1];
InternationalPhone ip = (InternationalPhone)teacher.phoneInfo;
Console.WriteLine(teacher.name);
Console.WriteLine(a.city);
Console.WriteLine(p.phoneNumber);
Console.WriteLine(ip.countryCode);
} //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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
.NET Framework
サポート対象 : 2.0、1.1、1.0
.NET Compact Framework
サポート対象 : 2.0
参照
関連項目
XmlSerializer クラス
XmlSerializer メンバ
System.Xml.Serialization 名前空間
XmlAttributes クラス
その他の技術情報
XML シリアル化の概要
方法 : XML ストリームの代替要素名を指定する
属性を使用した XML シリアル化の制御
XML シリアル化の例
XML スキーマ定義ツール (Xsd.exe)