XmlArrayAttribute Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menentukan bahwa XmlSerializer harus menserialisasikan anggota kelas tertentu sebagai array elemen 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
- Warisan
- Atribut
Contoh
Contoh berikut menserialisasikan instans kelas ke dalam dokumen XML yang berisi beberapa array objek. XmlArrayAttribute diterapkan ke anggota yang menjadi array elemen 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
Keterangan
XmlArrayAttribute milik keluarga atribut yang mengontrol bagaimana XmlSerializer serialisasi atau deserialisasi objek. Untuk daftar lengkap atribut serupa, lihat Atribut yang Mengontrol Serialisasi XML.
Anda dapat menerapkan ke XmlArrayAttribute bidang publik atau properti baca/tulis yang mengembalikan array objek. Anda juga dapat menerapkannya ke koleksi dan bidang yang mengembalikan ArrayList bidang atau bidang apa pun yang mengembalikan objek yang mengimplementasikan IEnumerable antarmuka.
Saat Anda menerapkan ke XmlArrayAttribute anggota kelas, Serialize metode kelas menghasilkan urutan elemen XML berlapis dari anggota tersebut XmlSerializer . Dokumen skema XML (file .xsd), menunjukkan array seperti complexType. Misalnya, jika kelas yang akan diserialisasikan mewakili pesanan pembelian, Anda dapat menghasilkan array item yang dibeli dengan menerapkan XmlArrayAttribute ke bidang publik yang mengembalikan array objek yang mewakili item pesanan.
Jika tidak ada atribut yang diterapkan ke bidang publik atau properti yang mengembalikan array objek jenis kompleks atau primitif, XmlSerializer menghasilkan urutan berlapis elemen XML secara default. Untuk lebih tepat mengontrol elemen XML apa yang dihasilkan, terapkan XmlArrayItemAttribute dan XmlArrayAttribute ke bidang atau properti. Misalnya, secara default, nama elemen XML yang dihasilkan berasal dari pengidentifikasi anggota Anda dapat mengubah nama elemen XML yang dihasilkan dengan mengatur ElementName properti .
Jika Anda membuat serial array yang berisi item dari jenis tertentu dan semua kelas yang berasal dari jenis tersebut XmlArrayItemAttribute , Anda harus menggunakan untuk mendeklarasikan setiap jenis.
Nota
Anda dapat menggunakan XmlArray dalam kode Anda alih-alih lebih lama XmlArrayAttribute.
Untuk informasi selengkapnya tentang menggunakan atribut, lihat Atribut.
Konstruktor
| Nama | Deskripsi |
|---|---|
| XmlArrayAttribute() |
Menginisialisasi instans baru dari kelas XmlArrayAttribute. |
| XmlArrayAttribute(String) |
Menginisialisasi instans XmlArrayAttribute baru kelas dan menentukan nama elemen XML yang dihasilkan dalam instans dokumen XML. |
Properti
| Nama | Deskripsi |
|---|---|
| ElementName |
Mendapatkan atau mengatur nama elemen XML yang diberikan ke array berseri. |
| Form |
Mendapatkan atau menetapkan nilai yang menunjukkan apakah nama elemen XML yang dihasilkan oleh XmlSerializer memenuhi syarat atau tidak memenuhi syarat. |
| IsNullable |
Mendapatkan atau menetapkan nilai yang menunjukkan apakah XmlSerializer harus menserialisasikan anggota sebagai tag XML kosong dengan atribut yang |
| Namespace |
Mendapatkan atau mengatur namespace elemen XML. |
| Order |
Mendapatkan atau mengatur urutan eksplisit di mana elemen diserialisasikan atau dideserialisasi. |
| TypeId |
Ketika diimplementasikan dalam kelas turunan, mendapatkan pengidentifikasi unik untuk Attributeini. (Diperoleh dari Attribute) |
Metode
| Nama | Deskripsi |
|---|---|
| Equals(Object) |
Mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu. (Diperoleh dari Attribute) |
| GetHashCode() |
Mengembalikan kode hash untuk instans ini. (Diperoleh dari Attribute) |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| IsDefaultAttribute() |
Ketika ditimpa dalam kelas turunan, menunjukkan apakah nilai instans ini adalah nilai default untuk kelas turunan. (Diperoleh dari Attribute) |
| Match(Object) |
Saat ditimpa dalam kelas turunan, mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu. (Diperoleh dari Attribute) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |
Implementasi Antarmuka Eksplisit
| Nama | Deskripsi |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Memetakan sekumpulan nama ke sekumpulan pengidentifikasi pengiriman yang sesuai. (Diperoleh dari Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Mengambil informasi jenis untuk objek, yang dapat digunakan untuk mendapatkan informasi jenis untuk antarmuka. (Diperoleh dari Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Mengambil jumlah antarmuka informasi jenis yang disediakan objek (baik 0 atau 1). (Diperoleh dari Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Menyediakan akses ke properti dan metode yang diekspos oleh objek. (Diperoleh dari Attribute) |