XmlArrayItemAttribute クラスとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > XmlArrayItemAttribute クラスの意味・解説 

XmlArrayItemAttribute クラス

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 または XmlElementAttribute の複数インスタンス適用して配列挿入できるオブジェクトの型を指定できます

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

メモメモ

コードでは、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) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlArrayItemAttribute メンバ
System.Xml.Serialization 名前空間
XmlArrayAttribute クラス
XmlSerializer
XmlArrayItems
XmlAttributeOverrides
XmlAttributes
XmlAttributes
その他の技術情報
XML シリアル化概要
方法 : XML ストリーム代替要素名を指定する
属性使用した XML シリアル化制御
XML シリアル化の例
XML スキーマ定義ツール (Xsd.exe)



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「XmlArrayItemAttribute クラス」の関連用語

XmlArrayItemAttribute クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



XmlArrayItemAttribute クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS