次の方法で共有


XmlArrayItemAttribute クラス

XmlSerializer がシリアル化された配列で配置できる派生型を指定します。

この型のすべてのメンバの一覧については、XmlArrayItemAttribute メンバ を参照してください。

System.Object
   System.Attribute
      System.Xml.Serialization.XmlArrayItemAttribute

<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field _
   Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue)>
Public Class XmlArrayItemAttribute   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field
   | AttributeTargets.Parameter | AttributeTargets.ReturnValue)]
public class XmlArrayItemAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Property |
   AttributeTargets::Field | AttributeTargets::Parameter |
   AttributeTargets::ReturnValue)]
public __gc class XmlArrayItemAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Property | AttributeTargets.Field |
   AttributeTargets.Parameter | AttributeTargets.ReturnValue)
class XmlArrayItemAttribute extends Attribute

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

XmlArrayItemAttribute は、 XmlSerializer がオブジェクトをシリアル化または逆シリアル化する方法を制御する一連の属性に属します。類似する属性の全一覧については、「 XML シリアル化を制御する属性 」を参照してください。

XmlArrayItemAttribute は、配列を返す任意のパブリック読み取り/書き込みメンバに適用でき、メンバ (たとえば、オブジェクトの配列を返すフィールド、コレクション、 ArrayList 、または IEnumerable インターフェイスを実装する任意のクラス) へのアクセスを提供します。

XmlArrayItemAttribute はポリモーフィズムをサポートしています。つまり XmlSerializer によって派生オブジェクトを配列に追加できます。たとえば、 Animal という基本クラスから派生した Mammal クラスがあるとします。また、 MyAnimals というクラスには Animal オブジェクトの配列を返すフィールドがあるとします。 XmlSerializer により Animal 型および Mammal 型の両方をシリアル化するには、使用可能な 2 つの型のいずれか一方を指定して、フィールドに XmlArrayItemAttribute を 2 回適用します。

メモ    XmlArrayItemAttribute または XmlElementAttribute の複数のインスタンスを適用して、配列に挿入できるオブジェクトの型を指定できます。

メモ   インターフェイスまたはインターフェイスの配列を返すフィールドまたはプロパティのシリアル化は、サポートされていません。

属性の使用方法の詳細については、「 属性を使用したメタデータの拡張 」を参照してください。

メモ   コードでは、 XmlArrayItemAttribute の代わりに XmlArrayItem という短い語を使用できます。

使用例

[Visual Basic, C#, C++] Group というクラスをシリアル化する例を次に示します。このクラスには、 Employee オブジェクトの配列を返す Employees というフィールドが含まれています。この例では 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


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

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

public __gc class Employee
{
public:
   String* Name;
};

public __gc class Manager:public Employee
{
public:
   int Level;
};

public __gc class Group
{  
   /* The XmlArrayItemAttribute allows the XmlSerializer to insert
   both the base type (Employee) and derived type (Manager) 
   into serialized arrays. */

public:
   [XmlArrayItem(__typeof(Manager)),
      XmlArrayItem(__typeof(Employee))]
   Employee* Employees[];

   /* Use the XmlArrayItemAttribute to specify types allowed
   in an array of Object items. */
   [XmlArray]
   [XmlArrayItem (__typeof(Int32),
      ElementName = S"MyNumber"),
      XmlArrayItem (__typeof(String),
      ElementName = S"MyString"),
      XmlArrayItem(__typeof(Manager))]
   Object* ExtraInfo[];
};   

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 = S"Consuela";
   manager->Level = 3;
   emp1->Name = S"Seiko";
   emp2->Name = S"Martina";
   Employee* emps[] = {manager, emp1, emp2};
   group->Employees = emps;

   // Creates an int and a string and assigns to ExtraInfo.
   Object* temp[] = {__box(43), S"Extra", manager};
   group->ExtraInfo = temp;

   // Serializes the object, and closes the StreamWriter.
   s->Serialize(writer, group);
   writer->Close();
}

void DeserializeObject(String* filename)
{
   FileStream* fs = new FileStream(filename, FileMode::Open);
   XmlSerializer* x = new XmlSerializer(__typeof(Group));
   Group* g = dynamic_cast<Group*> (x->Deserialize(fs));
   Console::WriteLine(S"Members:");

   System::Collections::IEnumerator* myEnum = g->Employees->GetEnumerator();
   while (myEnum->MoveNext())
   {
      Employee* e = __try_cast<Employee*>(myEnum->Current);
      Console::WriteLine(S"\t{0}", e->Name);
   }
}

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

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

必要条件

名前空間: System.Xml.Serialization

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

アセンブリ: System.Xml (System.Xml.dll 内)

参照

XmlArrayItemAttribute メンバ | System.Xml.Serialization 名前空間 | XmlArrayAttribute | XmlSerializer | XmlArrayItems | XmlAttributeOverrides | XmlAttributes | XML シリアル化の概要 | XML シリアル化のオーバーライド | XmlAttributes | 属性を使用した XML シリアル化の制御 | XML シリアル化の例