XmlSerializer がシリアル化された配列で配置できる派生型を指定します。
名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, AllowMultiple:=True)> _
Public Class XmlArrayItemAttribute
Inherits Attribute
Dim instance As XmlArrayItemAttribute
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=true)]
public class XmlArrayItemAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Property|AttributeTargets::Field|AttributeTargets::Parameter|AttributeTargets::ReturnValue, AllowMultiple=true)]
public ref class XmlArrayItemAttribute : public Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=true) */
public class XmlArrayItemAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=true)
public class XmlArrayItemAttribute extends Attribute
XmlArrayItemAttribute は、XmlSerializer がオブジェクトをシリアル化または逆シリアル化する方法を制御する一連の属性の 1 つです。類似する属性の完全な一覧については、「XML シリアル化を制御する属性」を参照してください。
XmlArrayItemAttribute は、配列を返したり配列へのアクセスを提供したりする任意のパブリックな読み取り/書き込みメンバに適用できます。たとえば、オブジェクトの配列を返すフィールド、コレクション、ArrayList、または IEnumerable インターフェイスを実装する任意のクラスがこれに該当します。
XmlArrayItemAttribute はポリモーフィズムをサポートしています。つまり XmlSerializer によって派生オブジェクトを配列に追加できます。たとえば、Animal という基本クラスから派生した Mammal クラスがあるとします。また、MyAnimals というクラスには Animal オブジェクトの配列を返すフィールドがあるとします。XmlSerializer により Animal 型および Mammal 型の両方をシリアル化するには、使用可能な 2 つの型のいずれか一方を指定して、フィールドに XmlArrayItemAttribute を 2 回適用します。
メモ |
|---|
| インターフェイスまたはインターフェイスの配列を返すフィールドまたはプロパティのシリアル化は、サポートされていません。 |
属性の使用方法については、「属性を使用したメタデータの拡張」を参照してください。
メモ |
|---|
| コードでは、XmlArrayItemAttribute の代わりに XmlArrayItem という短い語を使用できます。 |
Employee オブジェクトの配列を返す Employees というフィールドがある Group というクラスをシリアル化する例を次に示します。この例では XmlArrayItemAttribute をフィールドに適用します。これにより、XmlSerializer に対して、基本クラス (Employee) 型と派生クラス (Manager) 型の両方のオブジェクトをシリアル化された配列に挿入するように指示します。
Option Explicit
Option Strict
Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports Microsoft.VisualBasic
Public Class Group
' The XmlArrayItemAttribute allows the XmlSerializer to insert
' both the base type (Employee) and derived type (Manager)
' into serialized arrays.
<XmlArrayItem(GetType(Manager)), _
XmlArrayItem(GetType(Employee))> _
Public Employees() As Employee
' Use the XmlArrayItemAttribute to specify types allowed
' in an array of Object items.
<XmlArray(), _
XmlArrayItem(GetType(Integer), ElementName := "MyNumber"), _
XmlArrayItem(GetType(String), ElementName := "MyString"), _
XmlArrayItem(GetType(Manager))> _
Public ExtraInfo() As Object
End Class
Public Class Employee
Public Name As String
End Class
Public Class Manager
Inherits Employee
Public Level As Integer
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("TypeDoc.xml")
test.DeserializeObject("TypeDoc.xml")
End Sub
Public Sub SerializeObject(ByVal filename As String)
' Creates a new XmlSerializer.
Dim s As New XmlSerializer(GetType(Group))
' Writing the XML file to disk requires a TextWriter.
Dim writer As New StreamWriter(filename)
Dim group As New Group()
Dim manager As New Manager()
Dim emp1 As New Employee()
Dim emp2 As New Employee()
manager.Name = "Consuela"
manager.Level = 3
emp1.Name = "Seiko"
emp2.Name = "Martina"
Dim emps() As Employee = {manager, emp1, emp2}
group.Employees = emps
' Creates an int and a string and assigns to ExtraInfo.
group.ExtraInfo = New Object() {43, "Extra", manager}
' Serializes the object, and closes the StreamWriter.
s.Serialize(writer, group)
writer.Close()
End Sub
Public Sub DeserializeObject(ByVal filename As String)
Dim fs As New FileStream(filename, FileMode.Open)
Dim x As New XmlSerializer(GetType(Group))
Dim g As Group = CType(x.Deserialize(fs), Group)
Console.WriteLine("Members:")
Dim e As Employee
For Each e In g.Employees
Console.WriteLine(ControlChars.Tab & e.Name)
Next e
End Sub
End Class
using System;
using System.IO;
using System.Xml.Serialization;
public class Group
{
/* The XmlArrayItemAttribute allows the XmlSerializer to insert
both the base type (Employee) and derived type (Manager)
into serialized arrays. */
[XmlArrayItem(typeof(Manager)),
XmlArrayItem(typeof(Employee))]
public Employee[] Employees;
/* Use the XmlArrayItemAttribute to specify types allowed
in an array of Object items. */
[XmlArray]
[XmlArrayItem (typeof(int),
ElementName = "MyNumber"),
XmlArrayItem (typeof(string),
ElementName = "MyString"),
XmlArrayItem(typeof(Manager))]
public object [] ExtraInfo;
}
public class Employee
{
public string Name;
}
public class Manager:Employee{
public int Level;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("TypeDoc.xml");
test.DeserializeObject("TypeDoc.xml");
}
public void SerializeObject(string filename)
{
// Creates a new XmlSerializer.
XmlSerializer s = new XmlSerializer(typeof(Group));
// Writing the XML file to disk requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
Group group = new Group();
Manager manager = new Manager();
Employee emp1 = new Employee();
Employee emp2 = new Employee();
manager.Name = "Consuela";
manager.Level = 3;
emp1.Name = "Seiko";
emp2.Name = "Martina";
Employee [] emps = new Employee[3]{manager, emp1, emp2};
group.Employees = emps;
// Creates an int and a string and assigns to ExtraInfo.
group.ExtraInfo = new Object[3]{43, "Extra", manager};
// Serializes the object, and closes the StreamWriter.
s.Serialize(writer, group);
writer.Close();
}
public void DeserializeObject(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open);
XmlSerializer x = new XmlSerializer(typeof(Group));
Group g = (Group) x.Deserialize(fs);
Console.WriteLine("Members:");
foreach(Employee e in g.Employees)
{
Console.WriteLine("\t" + e.Name);
}
}
}
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
public ref class Employee
{
public:
String^ Name;
};
public ref class Manager: public Employee
{
public:
int Level;
};
public ref class Group
{
public:
/* The XmlArrayItemAttribute allows the XmlSerializer to insert
both the base type (Employee) and derived type (Manager)
into serialized arrays. */
[XmlArrayItem(Manager::typeid),
XmlArrayItem(Employee::typeid)]
array<Employee^>^Employees;
/* Use the XmlArrayItemAttribute to specify types allowed
in an array of Object items. */
[XmlArray]
[XmlArrayItem(Int32::typeid,
ElementName="MyNumber"),
XmlArrayItem(String::typeid,
ElementName="MyString"),
XmlArrayItem(Manager::typeid)]
array<Object^>^ExtraInfo;
};
void SerializeObject( String^ filename )
{
// Creates a new XmlSerializer.
XmlSerializer^ s = gcnew XmlSerializer( Group::typeid );
// Writing the XML file to disk requires a TextWriter.
TextWriter^ writer = gcnew StreamWriter( filename );
Group^ group = gcnew Group;
Manager^ manager = gcnew Manager;
Employee^ emp1 = gcnew Employee;
Employee^ emp2 = gcnew Employee;
manager->Name = "Consuela";
manager->Level = 3;
emp1->Name = "Seiko";
emp2->Name = "Martina";
array<Employee^>^emps = {manager,emp1,emp2};
group->Employees = emps;
// Creates an int and a string and assigns to ExtraInfo.
array<Object^>^temp = {43,"Extra",manager};
group->ExtraInfo = temp;
// Serializes the object, and closes the StreamWriter.
s->Serialize( writer, group );
writer->Close();
}
void DeserializeObject( String^ filename )
{
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
XmlSerializer^ x = gcnew XmlSerializer( Group::typeid );
Group^ g = dynamic_cast<Group^>(x->Deserialize( fs ));
Console::WriteLine( "Members:" );
System::Collections::IEnumerator^ myEnum = g->Employees->GetEnumerator();
while ( myEnum->MoveNext() )
{
Employee^ e = safe_cast<Employee^>(myEnum->Current);
Console::WriteLine( "\t{0}", e->Name );
}
}
int main()
{
SerializeObject( "TypeDoc.xml" );
DeserializeObject( "TypeDoc.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
public class Group
{
/* The XmlArrayItemAttribute allows the XmlSerializer to insert
both the base type (Employee) and derived type (Manager)
into serialized arrays.
*/
/** @attribute XmlArrayItem(Manager.class)
@attribute XmlArrayItem(Employee.class)
*/
public Employee employees[];
/* Use the XmlArrayItemAttribute to specify types allowed
in an array of Object items.
*/
/** @attribute XmlArray()
*/
/** @attribute XmlArrayItem(int.class, ElementName = "MyNumber")
@attribute XmlArrayItem(String.class, ElementName = "MyString")
@attribute XmlArrayItem(Manager.class)
*/
public Object extraInfo[];
} //Group
public class Employee
{
public String name;
} //Employee
public class Manager extends Employee
{
public int level;
} //Manager
public class Run
{
public static void main(String[] args)
{
Run test = new Run();
test.SerializeObject("TypeDoc.xml");
test.DeserializeObject("TypeDoc.xml");
} //main
public void SerializeObject(String fileName)
{
// Creates a new XmlSerializer.
XmlSerializer s = new XmlSerializer(Group.class.ToType());
// Writing the XML file to disk requires a TextWriter.
TextWriter writer = new StreamWriter(fileName);
Group group = new Group();
Manager manager = new Manager();
Employee emp1 = new Employee();
Employee emp2 = new Employee();
manager.name = "Consuela";
manager.level = 3;
emp1.name = "Seiko";
emp2.name = "Martina";
Employee emps[] = new Employee[] { manager, emp1, emp2 };
group.employees = emps;
// Creates an int and a string and assigns to ExtraInfo.
group.extraInfo = new Object[] { (Int32)43, "Extra", manager };
// Serializes the object, and closes the StreamWriter.
s.Serialize(writer, group);
writer.Close();
} //SerializeObject
public void DeserializeObject(String fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
XmlSerializer x = new XmlSerializer(Group.class.ToType());
Group g = (Group)(x.Deserialize(fs));
Console.WriteLine("Members:");
for (int iCtr = 0; iCtr < g.employees.length; iCtr++) {
Employee e = (Employee)g.employees.get_Item(iCtr);
Console.WriteLine("\t" + e.name);
}
} //DeserializeObject
} //Run
System.Object
System.Attribute
System.Xml.Serialization.XmlArrayItemAttribute
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
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、1.0