This documentation is archived and is not being maintained.

XmlArrayItemAttribute Class

Specifies the derived types that the XmlSerializer can place in a serialized array.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)

'Declaration
<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, AllowMultiple := True)> _
Public Class XmlArrayItemAttribute _
	Inherits Attribute
'Usage
Dim instance As XmlArrayItemAttribute

The XmlArrayItemAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object. For a complete list of similar attributes, see Attributes That Control XML Serialization.

You can apply the XmlArrayItemAttribute to any public read/write member that returns an array, or provides access to one. For example, a field that returns an array of objects, a collection, an ArrayList, or any class that implements the IEnumerable interface.

The XmlArrayItemAttribute supports polymorphism--in other words, it allows the XmlSerializer to add derived objects to an array. For example, suppose a class named Mammal is derived from a base class named Animal. Further suppose that a class named MyAnimals contains a field that returns an array of Animal objects. To allow the XmlSerializer to serialize both the Animal and Mammal type, apply the XmlArrayItemAttribute to the field twice, each time specifying one of the two acceptable types.

NoteNote:

You can apply multiple instances of the XmlArrayItemAttribute or XmlElementAttribute to specify types of objects that can be inserted into the array.

NoteNote:

The serialization of a field or property that returns an interface or array of interfaces is not supported.

For more information about using attributes, see Extending Metadata Using Attributes.

NoteNote:

You can use the word XmlArrayItem in your code instead of the longer XmlArrayItemAttribute.

The following example serializes a class named Group that contains a field named Employees that returns an array of Employee objects. The example applies the XmlArrayItemAttribute to the field, thereby instructing the XmlSerializer that it can insert objects of both the base class (Employee) type and derived class type (Manager) into the serialized array.

Option Explicit
Option Strict

Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports Microsoft.VisualBasic


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
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;

public __gc class Employee
{
public:
   String* Name;
};

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

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

public:
   [XmlArrayItem(__typeof(Manager)),
      XmlArrayItem(__typeof(Employee))]
   Employee* Employees[];

   /* Use the XmlArrayItemAttribute to specify types allowed
   in an array of Object items. */
   [XmlArray]
   [XmlArrayItem (__typeof(Int32),
      ElementName = S"MyNumber"),
      XmlArrayItem (__typeof(String),
      ElementName = S"MyString"),
      XmlArrayItem(__typeof(Manager))]
   Object* ExtraInfo[];
};   

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 = S"Consuela";
   manager->Level = 3;
   emp1->Name = S"Seiko";
   emp2->Name = S"Martina";
   Employee* emps[] = {manager, emp1, emp2};
   group->Employees = emps;

   // Creates an int and a string and assigns to ExtraInfo.
   Object* temp[] = {__box(43), S"Extra", manager};
   group->ExtraInfo = temp;

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

void DeserializeObject(String* filename)
{
   FileStream* fs = new FileStream(filename, FileMode::Open);
   XmlSerializer* x = new XmlSerializer(__typeof(Group));
   Group* g = dynamic_cast<Group*> (x->Deserialize(fs));
   Console::WriteLine(S"Members:");

   System::Collections::IEnumerator* myEnum = g->Employees->GetEnumerator();
   while (myEnum->MoveNext())
   {
      Employee* e = __try_cast<Employee*>(myEnum->Current);
      Console::WriteLine(S"\t{0}", e->Name);
   }
}

int main()
{
   SerializeObject(S"TypeDoc.xml");
   DeserializeObject(S"TypeDoc.xml");
}

System.Object
  System.Attribute
    System.Xml.Serialization.XmlArrayItemAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: