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 的类,该类包含返回对象数组的Employee名为 Employees 的字段。 该示例将 应用于 XmlArrayItemAttribute 字段,从而指示 XmlSerializer 它可以将基类 () Employee 类型和派生类类型的对象插入 (Manager) 序列化数组中。

#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public ref class Employee
{
public:
   String^ Name;
};

public ref class Manager: public Employee
{
public:
   int Level;
};

public ref class Group
{
public:

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

   [XmlArrayItem(Manager::typeid),
   XmlArrayItem(Employee::typeid)]
   array<Employee^>^Employees;

   /* Use the XmlArrayItemAttribute to specify types allowed
      in an array of Object items. */

   [XmlArray]
   [XmlArrayItem(Int32::typeid,
   ElementName="MyNumber"),
   XmlArrayItem(String::typeid,
   ElementName="MyString"),
   XmlArrayItem(Manager::typeid)]
   array<Object^>^ExtraInfo;
};

void SerializeObject( String^ filename )
{
   // Creates a new XmlSerializer.
   XmlSerializer^ s = gcnew XmlSerializer( Group::typeid );

   // Writing the XML file to disk requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );
   Group^ group = gcnew Group;
   Manager^ manager = gcnew Manager;
   Employee^ emp1 = gcnew Employee;
   Employee^ emp2 = gcnew Employee;
   manager->Name = "Consuela";
   manager->Level = 3;
   emp1->Name = "Seiko";
   emp2->Name = "Martina";
   array<Employee^>^emps = {manager,emp1,emp2};
   group->Employees = emps;

   // Creates an int and a string and assigns to ExtraInfo.
   array<Object^>^temp = {43,"Extra",manager};
   group->ExtraInfo = temp;

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

void DeserializeObject( String^ filename )
{
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   XmlSerializer^ x = gcnew XmlSerializer( Group::typeid );
   Group^ g = dynamic_cast<Group^>(x->Deserialize( fs ));
   Console::WriteLine( "Members:" );
   System::Collections::IEnumerator^ myEnum = g->Employees->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Employee^ e = safe_cast<Employee^>(myEnum->Current);
      Console::WriteLine( "\t{0}", e->Name );
   }
}

int main()
{
   SerializeObject( "TypeDoc.xml" );
   DeserializeObject( "TypeDoc.xml" );
}
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 字段两次,每次指定两个可接受的类型之一。

备注

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

备注

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

有关使用特性的详细信息,请参阅 特性

注意

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

构造函数

XmlArrayItemAttribute()

初始化 XmlArrayItemAttribute 类的新实例。

XmlArrayItemAttribute(String)

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

XmlArrayItemAttribute(String, Type)

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

XmlArrayItemAttribute(Type)

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

属性

DataType

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

ElementName

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

Form

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

IsNullable

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

Namespace

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

NestingLevel

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

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)

适用于

另请参阅