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.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Xml;
public ref class Item
{
public:
[XmlElement(ElementName="OrderItem")]
String^ ItemName;
String^ ItemCode;
Decimal ItemPrice;
int ItemQuantity;
};
public ref class BookItem: public Item
{
public:
String^ Title;
String^ Author;
String^ ISBN;
};
// This is the class that will be serialized.
public ref class MyRootClass
{
private:
array<Item^>^items;
public:
/* 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)]
array<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=Item::typeid,
Namespace="http://www.cpandl.com"),
XmlArrayItem(ElementName="BookItem",
IsNullable=true,
Type=BookItem::typeid,
Namespace="http://www.cohowinery.com")]
[XmlArray]
property array<Item^>^ Items
{
array<Item^>^ get()
{
return items;
}
void set( array<Item^>^value )
{
items = value;
}
}
};
public ref class Run
{
public:
void SerializeDocument( String^ filename )
{
// Creates a new XmlSerializer.
XmlSerializer^ s = gcnew XmlSerializer( MyRootClass::typeid );
// Writing the file requires a StreamWriter.
TextWriter^ myWriter = gcnew StreamWriter( filename );
// Creates an instance of the class to serialize.
MyRootClass^ myRootClass = gcnew MyRootClass;
/* Uses a basic method of creating an XML array: Create and
populate a string array, and assign it to the
MyStringArray property. */
array<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 = gcnew Item;
BookItem^ item2 = gcnew 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.
array<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();
}
};
int main()
{
Run^ test = gcnew Run;
test->SerializeDocument( "books.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 至類別成員時,類別 Serialize 的 XmlSerializer 方法會從該成員產生 XML 元素的巢狀序列。 XML 架構檔 (.xsd 檔案) ,表示這類陣列為 complexType
。 例如,如果要序列化的類別代表採購單,您可以將 套用 XmlArrayAttribute 至傳回代表訂單專案之物件的陣列,藉此產生已購買專案的陣列。
如果未將任何屬性套用至傳回復雜或基本類型物件陣列的公用欄位或屬性,預設 XmlSerializer 會產生 XML 元素的巢狀序列。 若要更精確地控制產生的 XML 元素,請將 和 XmlArrayAttribute 套用 XmlArrayItemAttribute 至欄位或屬性。 例如,根據預設,產生的 XML 專案名稱衍生自成員識別碼 您可以藉由設定 ElementName 屬性來變更所產生 XML 專案的名稱。
如果您序列化陣列,其中包含特定型別的專案和衍生自該型別的所有類別,則必須使用 XmlArrayItemAttribute 來宣告每個型別。
注意
您可以在程式碼中使用 XmlArray
,而不是較長 XmlArrayAttribute 的 。
如需使用屬性的詳細資訊,請參閱 屬性。
建構函式
XmlArrayAttribute() |
初始化 XmlArrayAttribute 類別的新執行個體。 |
XmlArrayAttribute(String) |
初始化 XmlArrayAttribute 類別的新執行個體,指定 XML 文件執行個體中產生的 XML 項目名稱。 |
屬性
ElementName |
取得或設定指定給序列化陣列的 XML 項目名稱。 |
Form |
取得或設定值,指出 XmlSerializer 產生的 XML 項目名稱是限定的還是非限定的。 |
IsNullable |
取得或設定值,指出 XmlSerializer 是否必須將成員序列化為 |
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) |
將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承來源 Attribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
擷取物件的類型資訊,可以用來取得介面的類型資訊。 (繼承來源 Attribute) |
_Attribute.GetTypeInfoCount(UInt32) |
擷取物件提供的類型資訊介面數目 (0 或 1)。 (繼承來源 Attribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供物件所公開的屬性和方法的存取權。 (繼承來源 Attribute) |