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 的类,该类包含一个名为 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 向数组添加派生对象。 例如,假设命名 的类派生自名为 的基类。 此外,假设一个名为 MyAnimals 的类包含返回对象数组的 Animal 字段。 若要允许XmlSerializer序列化和AnimalMammal类型,请对字段应用XmlArrayItemAttribute两次,每次指定两种可接受的类型之一。

注释

可以应用多个实例XmlArrayItemAttributeXmlElementAttribute或指定可插入到数组中的对象类型。

注释

不支持返回接口或接口数组的字段或属性的序列化。

有关使用属性的详细信息,请参阅 “属性”。

注释

可以在代码中使用单词 XmlArrayItem ,而不是较长 XmlArrayItemAttribute时间。

构造函数

名称 说明
XmlArrayItemAttribute()

初始化 XmlArrayItemAttribute 类的新实例。

XmlArrayItemAttribute(String, Type)

初始化类的新实例 XmlArrayItemAttribute ,并指定在 XML 文档中生成的 XML 元素的名称,以及 Type 可以插入到生成的 XML 文档中的 XML 元素的名称。

XmlArrayItemAttribute(String)

初始化类的新实例 XmlArrayItemAttribute ,并指定 XML 文档中生成的 XML 元素的名称。

XmlArrayItemAttribute(Type)

初始化类的新实例 XmlArrayItemAttribute ,并指定 Type 可插入到序列化数组中的实例。

属性

名称 说明
DataType

获取或设置生成的 XML 元素的 XML 数据类型。

ElementName

获取或设置生成的 XML 元素的名称。

Form

获取或设置一个值,该值指示生成的 XML 元素的名称是否限定。

IsNullable

获取或设置一个值,该值指示 XmlSerializer 必须将成员序列化为空 XML 标记,并将 xsi:nil 属性设置为 true

Namespace

获取或设置生成的 XML 元素的命名空间。

NestingLevel

获取或设置影响 XML 元素 XmlArrayItemAttribute 层次结构中的级别。

Type

获取或设置数组中允许的类型。

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)

适用于

另请参阅