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 用到 public field,或是用 read/write 屬性來回傳一組物件。 你也可以把它套用到回傳一個 ArrayList 集合和欄位,或任何回傳實作 IEnumerable 介面物件的欄位。
當你將 套用到 類別 XmlArrayAttribute 成員時, Serialize 該類別的方法 XmlSerializer 會從該成員產生巢狀的 XML 元素序列。 XML 結構文件(.xsd 檔案)表示此類陣列為 complexType。 例如,如果要序列化的類別代表採購訂單,你可以透過將 套 XmlArrayAttribute 用到一個公用欄位來產生已購買物品的陣列,該欄位會回傳代表訂單項目的物件陣列。
如果沒有屬性套用到一個公用欄位或屬性,該屬性會回傳複雜或原始型別物件陣列,預設 XmlSerializer 會產生巢狀的 XML 元素序列。 為了更精確地控制產生哪些 XML 元素,可以在欄位或屬性上套用 an XmlArrayItemAttribute 和 an XmlArrayAttribute 。 例如,預設情況下,產生的 XML 元素名稱是從成員識別碼衍生而來。你可以透過設定 ElementName 屬性來更改產生的 XML 元素名稱。
如果你序列化一個包含特定類型項目及其衍生所有類別的陣列,你必須使用 來 XmlArrayItemAttribute 宣告每個類型。
備註
你可以在程式碼中使用 XmlArray ,而不是較長 XmlArrayAttribute的 。
欲了解更多屬性的使用資訊,請參閱屬性。
建構函式
| 名稱 | Description |
|---|---|
| XmlArrayAttribute() |
初始化 XmlArrayAttribute 類別的新執行個體。 |
| XmlArrayAttribute(String) |
初始化該類別的新實例 XmlArrayAttribute ,並指定在 XML 文件實例中產生的 XML 元素名稱。 |
屬性
| 名稱 | Description |
|---|---|
| ElementName |
取得或設定序列化陣列所指定的 XML 元素名稱。 |
| Form |
取得或設定一個值,指示由 產生 XmlSerializer 的 XML 元素名稱是限定還是未限定。 |
| IsNullable |
取得或設定一個值,指示是否 XmlSerializer 必須將成員序列化為空的 XML 標籤,屬性 |
| Namespace |
取得或設定 XML 元素的命名空間。 |
| Order |
取得或設定元素序列化或非序列化的明確順序。 |
| TypeId |
在衍生類別中實作時,取得這個 Attribute的唯一標識碼。 (繼承來源 Attribute) |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
傳回值,這個值表示這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| GetHashCode() |
傳回這個實例的哈希碼。 (繼承來源 Attribute) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsDefaultAttribute() |
在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。 (繼承來源 Attribute) |
| Match(Object) |
在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| _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) |