XmlArrayItemAttribute クラス

定義

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

public ref class XmlArrayItemAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)]
public class XmlArrayItemAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)>]
type XmlArrayItemAttribute = class
    inherit Attribute
Public Class XmlArrayItemAttribute
Inherits Attribute
継承
XmlArrayItemAttribute
属性

次の例では、 オブジェクトの配列を返す という名前 Group のフィールドを含む という名前 EmployeesEmployee クラスをシリアル化します。 この例では、 フィールドに を適用XmlArrayItemAttributeし、基底クラス () 型と派生クラス型 (EmployeeManager) の両方のオブジェクトをシリアル化された配列に挿入できることを に指示XmlSerializerします。

#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" );
}
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);
      }
   }
}
Option Explicit
Option Strict

Imports System.IO
Imports 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(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

注釈

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

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

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

Note

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

Note

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

属性の使用の詳細については、「 属性」を参照してください。

Note

長い XmlArrayItemAttributeではなく、コードで 単語XmlArrayItemを使用できます。

コンストラクター

XmlArrayItemAttribute()

XmlArrayItemAttribute クラスの新しいインスタンスを初期化します。

XmlArrayItemAttribute(String)

XmlArrayItemAttribute クラスの新しいインスタンスを初期化し、XML ドキュメントで生成される XML 要素の名前を指定します。

XmlArrayItemAttribute(String, Type)

XmlArrayItemAttribute クラスの新しいインスタンスを初期化し、XML ドキュメントで生成される XML 要素の名前、および生成される XML ドキュメントに挿入できる Type を指定します。

XmlArrayItemAttribute(Type)

XmlArrayItemAttribute クラスの新しいインスタンスを初期化し、シリアル化される配列に挿入できる Type を指定します。

プロパティ

DataType

生成された XML 要素の XML データ型を取得または設定します。

ElementName

生成された XML 要素の名前を取得または設定します。

Form

生成された XML 要素名が修飾されているかどうかを示す値を取得または設定します。

IsNullable

XmlSerializer で、xsi:nil 属性が true に設定された空の XML タグとしてメンバーをシリアル化する必要があるかどうかを示す値を取得または設定します。

Namespace

生成された XML 要素の名前空間を取得または設定します。

NestingLevel

XmlArrayItemAttribute が影響を与える XML 要素の階層構造のレベルを取得または設定します。

Type

配列内で使用できる型を取得または設定します。

TypeId

派生クラスで実装されると、この Attribute の一意の識別子を取得します。

(継承元 Attribute)

メソッド

Equals(Object)

このインスタンスが、指定されたオブジェクトと等価であるかどうかを示す値を返します。

(継承元 Attribute)
GetHashCode()

このインスタンスのハッシュ コードを返します。

(継承元 Attribute)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IsDefaultAttribute()

派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

(継承元 Attribute)
Match(Object)

派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。

(継承元 Attribute)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

(継承元 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。

(継承元 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

(継承元 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。

(継承元 Attribute)

適用対象

こちらもご覧ください