XmlArrayItemAttribute.Type プロパティとは? わかりやすく解説

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

XmlArrayItemAttribute.Type プロパティ

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

名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文構文

解説解説

Type プロパティ使用してパブリック フィールドまたはパブリック読み取り/書き込みプロパティ値についてオーバーライドされる型を指定します

フィールドまたはプロパティObject 型の配列返す場合は、XmlArrayItemAttribute の複数インスタンスフィールドまたはプロパティ適用します。インスタンスType プロパティに、配列挿入できるオブジェクトの型を設定します

配列プリミティブ型だけを格納する場合XmlArrayItemAttribute適用する要はありません。既定では、XmlSerializer は個々の値に対してすべて同じ要素名で一連の要素生成しますが、各要素の型は XML スキーマ データ型設定されます。コード例次に示します

 ' Visual Basic code
 Public Class Arrays
    Public XSDTypes ()As Object= New Object(){"one", 2, 3.0}
 End Class
 // C# code
 public class MyArray{
    // No XmlArrayItemAttribute is applied.
    public object[] XSDTypes= new object[]{"one", 2, 3.2};
 }

結果XML次に示します

 <?xml version="1.0" encoding="utf-8"?>
 <Arrays xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <XSDTypes>
     <Object xsi:type="xsd:string">one</Object>
     <Object xsi:type="xsd:int">2</Object>
     <Object xsi:type="xsd:double">3</Object>
   </XSDTypes>
 </Arrays>

ただし、各プリミティブ型Type プロパティ指定した場合は、各値の要素名は .NET 型名使用して生成されます。コード例次に示します

 ' Visual Basic code
 Public Class Arrays
    <XmlArrayItem(GetType(String)), _
    XmlArrayItem(GetType(Integer)), _
    XmlArrayItem(GetType(Double))> _
    Public PrimitiveTypes () As Object = New Object(){"one", 2, 3.0}
 End Class
 // C# code
 public class Arrays{
    [XmlArrayItem(typeof(string))]
    [XmlArrayItem(typeof(int))]
    [XmlArrayItem(typeof(double))]
    public object [] PrimitiveTypes = new object[]{"one", 2, 3.0};
 }

結果XML次に示します

 <?xml version="1.0" encoding="utf-8"?>
 <Arrays xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <PrimitiveTypes>
     <string>one</string>
     <int>2</int>
     <double>3</double>
   </PrimitiveTypes>
 </Arrays>
使用例使用例

オブジェクト配列シリアル化する例を次に示します配列返すフィールド2 つXmlArrayItemAttribute インスタンスにより属性設定されています。それぞれのインスタンスXmlSerializer に対して配列内の指定されType受け入れるように指示します

Imports System
Imports System.IO
Imports System.Xml.Serialization



Public Class Group
    ' The Type property instructs the XmlSerializer to accept both
    ' the Person and Manager types in the array. 
    <XmlArrayItem(Type := GetType(Manager)), _
     XmlArrayItem(Type := GetType(Person))> _
    Public Staff() As Person
        
End Class 'Group


Public Class Person
    Public Name As String
End Class 


Public Class Manager
    Inherits Person
    Public Rank As Integer
End Class 


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New
 Run()
        test.SerializeOrder("TypeEx.xml")
    End Sub 
        
    
    Public Sub SerializeOrder(filename As
 String)
        ' Creates an XmlSerializer.
        Dim xSer As New
 XmlSerializer(GetType(Group))
        
        ' Creates the Group object, and two array items.
        Dim myGroup As New
 Group()
        
        Dim p1 As New Person()
        p1.Name = "Jacki"
        Dim p2 As New Manager()
        
        p2.Name = "Megan"
        p2.Rank = 2
        
        Dim myStaff() As Person =  {p1, p2}
        myGroup.Staff = myStaff
        
        ' Serializes the object, and closes the StreamWriter.
        Dim writer As New
 StreamWriter(filename)
        xSer.Serialize(writer, myGroup)
    End Sub 
End Class 'Run
using System;
using System.IO;
using System.Xml.Serialization;

public class Group
{
   /* The Type property instructs the XmlSerializer to accept both
   the Person and Manager types in the array. */
   [XmlArrayItem(Type = typeof(Manager)),
   XmlArrayItem(Type=typeof(Person))]
   public Person[]Staff;
}

public class Person
{
   public string Name;
}

public class Manager:Person
{
   public int Rank;
}

public class Run 
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeOrder("TypeEx.xml");
   }


   public void SerializeOrder(string
 filename)
   {
      // Creates an XmlSerializer.
      XmlSerializer xSer = 
      new XmlSerializer(typeof(Group));

      // Creates the Group object, and two array items.
      Group myGroup = new Group();

      Person p1 = new Person();
      p1.Name = "Jacki";
      Manager p2 = new Manager();

      p2.Name = "Megan";
      p2.Rank = 2;

      Person [] myStaff = {p1,p2};
      myGroup.Staff = myStaff;

      // Serializes the object, and closes the StreamWriter.
      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myGroup);
   }
}
#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public ref class Person
{
public:
   String^ Name;
};

public ref class Manager: public
 Person
{
public:
   int Rank;
};

public ref class Group
{
public:

   /* The Type property instructs the XmlSerializer to accept both
      the Person and Manager types in the array. */

   [XmlArrayItem(Type=Manager::typeid),
   XmlArrayItem(Type=Person::typeid)]
   array<Person^>^Staff;
};

void SerializeOrder( String^ filename )
{
   // Creates an XmlSerializer.
   XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid );

   // Creates the Group object, and two array items.
   Group^ myGroup = gcnew Group;
   Person^ p1 = gcnew Person;
   p1->Name = "Jacki";
   Manager^ p2 = gcnew Manager;
   p2->Name = "Megan";
   p2->Rank = 2;
   array<Person^>^myStaff = {p1,p2};
   myGroup->Staff = myStaff;
   
   // Serializes the object, and closes the StreamWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );
   xSer->Serialize( writer, myGroup );
}

int main()
{
   SerializeOrder( "TypeEx.xml" );
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;

public class Group
{
    /* The Type property instructs the XmlSerializer to accept both
       the Person and Manager types in the array. 
     */
    /** @attribute XmlArrayItem(Type = Manager.class)
        @attribute XmlArrayItem(Type = Person.class)
     */
    public Person staff[];
} //Group

public class Person
{
    public String name;
}//Person

public class Manager extends Person
{
    public int rank;
} //Manager

public class Run
{
    public static void main(String[]
 args)
    {
        Run test = new Run();
        test.SerializeOrder("TypeEx.xml");
    } //main

    public void SerializeOrder(String fileName)
    {
        // Creates an XmlSerializer.
        XmlSerializer xSer = new XmlSerializer(Group.class.ToType());

        // Creates the Group object, and two array items.
        Group myGroup = new Group();
        Person p1 = new Person();
        p1.name = "Jacki";

        Manager p2 = new Manager();
        p2.name = "Megan";
        p2.rank = 2;

        Person myStaff[] =  { p1, p2 };
        myGroup.staff = myStaff;

        // Serializes the object, and closes the StreamWriter.
        TextWriter writer = new StreamWriter(fileName);
        xSer.Serialize(writer, myGroup);
    } //SerializeOrder
} //Run
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
XmlArrayItemAttribute クラス
XmlArrayItemAttribute メンバ
System.Xml.Serialization 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からXmlArrayItemAttribute.Type プロパティを検索した結果を表示しています。
Weblioに収録されているすべての辞書からXmlArrayItemAttribute.Type プロパティを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からXmlArrayItemAttribute.Type プロパティ を検索

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

辞書ショートカット

すべての辞書の索引

「XmlArrayItemAttribute.Type プロパティ」の関連用語

XmlArrayItemAttribute.Type プロパティのお隣キーワード
検索ランキング

   

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



XmlArrayItemAttribute.Type プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS