XmlArrayAttribute クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
XmlSerializer が特定のクラス メンバーを XML 要素の配列としてシリアル化する必要があることを指定します。
public ref class XmlArrayAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=false)]
public class XmlArrayAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=false)>]
type XmlArrayAttribute = class
inherit Attribute
Public Class XmlArrayAttribute
Inherits Attribute
- 継承
- 属性
例
次の例では、複数のオブジェクト配列を含む XML ドキュメントにクラス インスタンスをシリアル化します。 XmlArrayAttributeは、XML 要素配列になるメンバーに適用されます。
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeDocument("books.xml");
}
public void SerializeDocument(string filename)
{
// Creates a new XmlSerializer.
XmlSerializer s =
new XmlSerializer(typeof(MyRootClass));
// Writing the file requires a StreamWriter.
TextWriter myWriter= new StreamWriter(filename);
// Creates an instance of the class to serialize.
MyRootClass myRootClass = new MyRootClass();
/* Uses a basic method of creating an XML array: Create and
populate a string array, and assign it to the
MyStringArray property. */
string [] myString = {"Hello", "world", "!"};
myRootClass.MyStringArray = myString;
/* Uses a more advanced method of creating an array:
create instances of the Item and BookItem, where BookItem
is derived from Item. */
Item item1 = new Item();
BookItem item2 = new BookItem();
// Sets the objects' properties.
item1.ItemName = "Widget1";
item1.ItemCode = "w1";
item1.ItemPrice = 231;
item1.ItemQuantity = 3;
item2.ItemCode = "w2";
item2.ItemPrice = 123;
item2.ItemQuantity = 7;
item2.ISBN = "34982333";
item2.Title = "Book of Widgets";
item2.Author = "John Smith";
// Fills the array with the items.
Item [] myItems = {item1,item2};
// Sets the class's Items property to the array.
myRootClass.Items = myItems;
/* Serializes the class, writes it to disk, and closes
the TextWriter. */
s.Serialize(myWriter, myRootClass);
myWriter.Close();
}
}
// This is the class that will be serialized.
public class MyRootClass
{
private Item [] items;
/* Here is a simple way to serialize the array as XML. Using the
XmlArrayAttribute, assign an element name and namespace. The
IsNullable property determines whether the element will be
generated if the field is set to a null value. If set to true,
the default, setting it to a null value will cause the XML
xsi:null attribute to be generated. */
[XmlArray(ElementName = "MyStrings",
Namespace = "http://www.cpandl.com", IsNullable = true)]
public string[] MyStringArray;
/* Here is a more complex example of applying an
XmlArrayAttribute. The Items property can contain both Item
and BookItem objects. Use the XmlArrayItemAttribute to specify
that both types can be inserted into the array. */
[XmlArrayItem(ElementName= "Item",
IsNullable=true,
Type = typeof(Item),
Namespace = "http://www.cpandl.com"),
XmlArrayItem(ElementName = "BookItem",
IsNullable = true,
Type = typeof(BookItem),
Namespace = "http://www.cohowinery.com")]
[XmlArray]
public Item []Items
{
get{return items;}
set{items = value;}
}
}
public class Item{
[XmlElement(ElementName = "OrderItem")]
public string ItemName;
public string ItemCode;
public decimal ItemPrice;
public int ItemQuantity;
}
public class BookItem:Item
{
public string Title;
public string Author;
public string ISBN;
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeDocument("books.xml")
End Sub
Public Sub SerializeDocument(ByVal filename As String)
' Creates a new XmlSerializer.
Dim s As New XmlSerializer(GetType(MyRootClass))
' Writing the file requires a StreamWriter.
Dim myWriter As New StreamWriter(filename)
' Creates an instance of the class to serialize.
Dim myRootClass As New MyRootClass()
' Uses a basic method of creating an XML array: Create and
' populate a string array, and assign it to the
' MyStringArray property.
Dim myString() As String = {"Hello", "world", "!"}
myRootClass.MyStringArray = myString
' Uses a more advanced method of creating an array:
' create instances of the Item and BookItem, where BookItem
' is derived from Item.
Dim item1 As New Item()
Dim item2 As New BookItem()
' Sets the objects' properties.
With item1
.ItemName = "Widget1"
.ItemCode = "w1"
.ItemPrice = 231
.ItemQuantity = 3
End With
With item2
.ItemCode = "w2"
.ItemPrice = 123
.ItemQuantity = 7
.ISBN = "34982333"
.Title = "Book of Widgets"
.Author = "John Smith"
End With
' Fills the array with the items.
Dim myItems() As Item = {item1, item2}
' Set class's Items property to the array.
myRootClass.Items = myItems
' Serializes the class, writes it to disk, and closes
' the TextWriter.
s.Serialize(myWriter, myRootClass)
myWriter.Close()
End Sub
End Class
' This is the class that will be serialized.
Public Class MyRootClass
Private myItems() As Item
' Here is a simple way to serialize the array as XML. Using the
' XmlArrayAttribute, assign an element name and namespace. The
' IsNullable property determines whether the element will be
' generated if the field is set to a null value. If set to true,
' the default, setting it to a null value will cause the XML
' xsi:null attribute to be generated.
<XmlArray(ElementName := "MyStrings", _
Namespace := "http://www.cpandl.com", _
IsNullable := True)> _
Public MyStringArray() As String
' Here is a more complex example of applying an
' XmlArrayAttribute. The Items property can contain both Item
' and BookItem objects. Use the XmlArrayItemAttribute to specify
' that both types can be inserted into the array.
<XmlArrayItem(ElementName := "Item", _
IsNullable := True, _
Type := GetType(Item), _
Namespace := "http://www.cpandl.com"), _
XmlArrayItem(ElementName := "BookItem", _
IsNullable := True, _
Type := GetType(BookItem), _
Namespace := "http://www.cohowinery.com"), _
XmlArray()> _
Public Property Items As Item()
Get
Return myItems
End Get
Set
myItems = value
End Set
End Property
End Class
Public Class Item
<XmlElement(ElementName := "OrderItem")> _
Public ItemName As String
Public ItemCode As String
Public ItemPrice As Decimal
Public ItemQuantity As Integer
End Class
Public Class BookItem
Inherits Item
Public Title As String
Public Author As String
Public ISBN As String
End Class
注釈
XmlArrayAttributeは、XmlSerializerがオブジェクトをシリアル化または逆シリアル化する方法を制御する属性のファミリに属しています。 類似する属性の完全な一覧については、「 XML シリアル化を制御する属性」を参照してください。
パブリック フィールドまたはオブジェクトの配列を返す読み取り/書き込みプロパティに XmlArrayAttribute を適用できます。 ArrayListを返すコレクションとフィールド、またはIEnumerable インターフェイスを実装するオブジェクトを返す任意のフィールドに適用することもできます。
XmlArrayAttributeをクラス メンバーに適用すると、XmlSerializer クラスの Serialize メソッドによって、そのメンバーから入れ子になった XML 要素のシーケンスが生成されます。 XML スキーマ ドキュメント (.xsd ファイル) は、このような配列を complexTypeとして示します。 たとえば、シリアル化するクラスが発注書を表す場合は、注文品目を表すオブジェクトの配列を返すパブリック フィールドに XmlArrayAttribute を適用することで、購入済みアイテムの配列を生成できます。
複合型オブジェクトまたはプリミティブ型オブジェクトの配列を返すパブリック フィールドまたはプロパティに属性が適用されていない場合、 XmlSerializer は XML 要素の入れ子になったシーケンスを既定で生成します。 生成される XML 要素をより正確に制御するには、フィールドまたはプロパティに XmlArrayItemAttribute と XmlArrayAttribute を適用します。 たとえば、既定では、生成された XML 要素の名前はメンバー識別子から派生します。生成された XML 要素の名前は、 ElementName プロパティを設定することで変更できます。
特定の型の項目とその型から派生したすべてのクラスを含む配列をシリアル化する場合は、 XmlArrayItemAttribute を使用して各型を宣言する必要があります。
注
長いXmlArrayAttributeではなく、コードでXmlArrayを使用できます。
属性の使用の詳細については、「 属性」を参照してください。
コンストラクター
| 名前 | 説明 |
|---|---|
| XmlArrayAttribute() |
XmlArrayAttribute クラスの新しいインスタンスを初期化します。 |
| XmlArrayAttribute(String) |
XmlArrayAttribute クラスの新しいインスタンスを初期化し、XML ドキュメント インスタンスで生成される XML 要素名を指定します。 |
プロパティ
| 名前 | 説明 |
|---|---|
| ElementName |
シリアル化された配列に指定された XML 要素名を取得または設定します。 |
| Form |
XmlSerializerによって生成された XML 要素名が修飾されているか、修飾されていないかを示す値を取得または設定します。 |
| IsNullable |
|
| Namespace |
XML 要素の名前空間を取得または設定します。 |
| Order |
要素がシリアル化または逆シリアル化される明示的な順序を取得または設定します。 |
| 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) |