XmlArrayItemAttribute 類別

定義

表示屬性,指定 XmlSerializer 可以放在串行化陣列中的衍生型別。

public ref class XmlArrayItemAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)]
public class XmlArrayItemAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property | System.AttributeTargets.ReturnValue, AllowMultiple=true)>]
type XmlArrayItemAttribute = class
    inherit Attribute
Public Class XmlArrayItemAttribute
Inherits Attribute
繼承
XmlArrayItemAttribute
屬性

範例

以下範例序列化一個名為 Group 的類別,該類別包含一個名為 named Employees 的欄位,該欄位回傳一個物件 Employee 陣列。 範例將 應用 XmlArrayItemAttribute 於欄位,指示 它 XmlSerializer 能將基底類別(Employee)型別與衍生類別型別(Manager)的物件插入序列化陣列。

using System;
using System.IO;
using System.Xml.Serialization;

public class Group
{
   /* The XmlArrayItemAttribute allows the XmlSerializer to insert
      both the base type (Employee) and derived type (Manager)
      into serialized arrays. */

   [XmlArrayItem(typeof(Manager)),
   XmlArrayItem(typeof(Employee))]
   public Employee[] Employees;

   /* Use the XmlArrayItemAttribute to specify types allowed
      in an array of Object items. */
   [XmlArray]
   [XmlArrayItem (typeof(int),
   ElementName = "MyNumber"),
   XmlArrayItem (typeof(string),
   ElementName = "MyString"),
   XmlArrayItem(typeof(Manager))]
   public object [] ExtraInfo;
}

public class Employee
{
   public string Name;
}

public class Manager:Employee{
   public int Level;
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("TypeDoc.xml");
      test.DeserializeObject("TypeDoc.xml");
   }

   public void SerializeObject(string filename)
   {
      // Creates a new XmlSerializer.
      XmlSerializer s = new XmlSerializer(typeof(Group));

      // Writing the XML file to disk requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);
      Group group = new Group();

      Manager manager = new Manager();
      Employee emp1 = new Employee();
      Employee emp2 = new Employee();
      manager.Name = "Consuela";
      manager.Level = 3;
      emp1.Name = "Seiko";
      emp2.Name = "Martina";
      Employee [] emps = new Employee[3]{manager, emp1, emp2};
      group.Employees = emps;

      // Creates an int and a string and assigns to ExtraInfo.
      group.ExtraInfo = new Object[3]{43, "Extra", manager};

      // Serializes the object, and closes the StreamWriter.
      s.Serialize(writer, group);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      FileStream fs = new FileStream(filename, FileMode.Open);
      XmlSerializer x = new XmlSerializer(typeof(Group));
      Group g = (Group) x.Deserialize(fs);
      Console.WriteLine("Members:");

      foreach(Employee e in g.Employees)
      {
         Console.WriteLine("\t" + e.Name);
      }
   }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml.Serialization

Public Class Group
    ' The XmlArrayItemAttribute allows the XmlSerializer to insert
    ' both the base type (Employee) and derived type (Manager)
    ' into serialized arrays. 
    
    <XmlArrayItem(GetType(Manager)), _
     XmlArrayItem(GetType(Employee))> _
    Public Employees() As Employee
    
    ' Use the XmlArrayItemAttribute to specify types allowed
    ' in an array of Object items. 
    <XmlArray(), _
     XmlArrayItem(GetType(Integer), ElementName := "MyNumber"), _
     XmlArrayItem(GetType(String), ElementName := "MyString"), _
     XmlArrayItem(GetType(Manager))> _
    Public ExtraInfo() As Object
End Class

Public Class Employee
    Public Name As String
End Class

Public Class Manager
    Inherits Employee
    Public Level As Integer
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("TypeDoc.xml")
        test.DeserializeObject("TypeDoc.xml")
    End Sub
    
       
    Public Sub SerializeObject(ByVal filename As String)
        ' Creates a new XmlSerializer.
        Dim s As New XmlSerializer(GetType(Group))
        
        ' Writing the XML file to disk requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        Dim group As New Group()
        
        Dim manager As New Manager()
        Dim emp1 As New Employee()
        Dim emp2 As New Employee()
        manager.Name = "Consuela"
        manager.Level = 3
        emp1.Name = "Seiko"
        emp2.Name = "Martina"
        Dim emps() As Employee = {manager, emp1, emp2}
        group.Employees = emps
        
        ' Creates an int and a string and assigns to ExtraInfo.
        group.ExtraInfo = New Object() {43, "Extra", manager}
        
        ' Serializes the object, and closes the StreamWriter.
        s.Serialize(writer, group)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(ByVal filename As String)
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim x As New XmlSerializer(GetType(Group))
        Dim g As Group = CType(x.Deserialize(fs), Group)
        Console.WriteLine("Members:")
        
        Dim e As Employee
        For Each e In  g.Employees
            Console.WriteLine(ControlChars.Tab & e.Name)
        Next e
    End Sub
End Class

備註

屬於 XmlArrayItemAttribute 一組屬性,控制物件如何 XmlSerializer 序列化或反序列化。 如需類似屬性的完整清單,請參閱 控制 XML 串行化的屬性

你可以將 套用到 任何 XmlArrayItemAttribute 回傳陣列或提供存取權限的公開讀寫成員。 例如,回傳物件陣列的欄位、集合、 ArrayList或任何實作該 IEnumerable 介面的類別。

XmlArrayItemAttribute支援多態性——換句話說,它允許將XmlSerializer導出物件加入陣列。 例如,假設一個名為 Mammal 的類別是由一個名為 Animal的基底類別推導而來。 進一步假設一個名為 MyAnimals 的類別包含一個回傳物件陣列 Animal 的欄位。 為了讓 能 XmlSerializer 同時序列化 AnimalMammal 型別,請將 套用 XmlArrayItemAttribute 兩次,每次指定兩個可接受型別中的一種。

Note

你可以套 XmlArrayItemAttribute 用多個 or XmlElementAttribute 實例,指定可插入陣列的物件類型。

Note

不支援回傳介面或介面陣列的欄位或屬性的序列化。

欲了解更多屬性的使用資訊,請參閱屬性。

Note

你可以在程式碼中使用這個詞 XmlArrayItem ,而不是較長 XmlArrayItemAttribute的 。

建構函式

名稱 Description
XmlArrayItemAttribute()

初始化 XmlArrayItemAttribute 類別的新執行個體。

XmlArrayItemAttribute(String, Type)

初始化該類別的新實例XmlArrayItemAttribute,並指定 XML 文件中產生的 XML 元素名稱,以及可插入生成的 XML 文件中的 。Type

XmlArrayItemAttribute(String)

初始化該類別的新實例 XmlArrayItemAttribute ,並指定 XML 文件中產生的 XML 元素名稱。

XmlArrayItemAttribute(Type)

初始化該類別的新實例XmlArrayItemAttribute,並指定可插入序列化陣列的 。Type

屬性

名稱 Description
DataType

取得或設定所產生 XML 元素的 XML 資料型別。

ElementName

取得或設定產生的 XML 元素名稱。

Form

取得或設定一個值,指示產生的 XML 元素名稱是否被限定。

IsNullable

取得或設定一個值,指示是否 XmlSerializer 必須將成員序列化為空的 XML 標籤,屬性 xsi:nil 設為 true

Namespace

取得或設定產生的 XML 元素的命名空間。

NestingLevel

取得或設定 影響的 XmlArrayItemAttribute XML 元素階層層級。

Type

取得或設定陣列中允許的型別。

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)

適用於

另請參閱