This documentation is archived and is not being maintained.

XmlArrayItemAttribute.Type Property

Gets or sets the type allowed in an array.

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

public Type Type { get; set; }

Property Value

Type: System.Type
A Type that is allowed in the array.

Use the Type property to specify an overridden type for a public field or public read/write property value.

If a field or property returns an array of type Object, apply multiple instances of the XmlArrayItemAttribute to the field or property. For each instance, set the Type property to a type of object that can be inserted into the array.

If an array contains only primitive types, you do not need to apply the XmlArrayItemAttribute. By default, the XmlSerializer generates a series of elements, each with the same element name, for each value, but the type of each element is set to the XML Schema data type. For example, the following code:

 ' Visual Basic code
 Public Class Arrays
    Public XSDTypes ()As Object= New Object(){"one", 2, 3.0}
 End Class
 // C# code
 public class MyArray{
    // No XmlArrayItemAttribute is applied.
    public object[] XSDTypes= new object[]{"one", 2, 3.2};
 }

results in this XML:

 <?xml version="1.0" encoding="utf-8"?>
 <Arrays xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <XSDTypes>
     <Object xsi:type="xsd:string">one</Object>
     <Object xsi:type="xsd:int">2</Object>
     <Object xsi:type="xsd:double">3</Object>
   </XSDTypes>
 </Arrays>

However, if you specify the Type property for each primitive type, the element name for each value is generated using the .NET type name. For example this code:

 ' Visual Basic code
 Public Class Arrays
    <XmlArrayItem(GetType(String)), _
    XmlArrayItem(GetType(Integer)), _
    XmlArrayItem(GetType(Double))> _
    Public PrimitiveTypes () As Object = New Object(){"one", 2, 3.0}
 End Class
 // C# code
 public class Arrays{
    [XmlArrayItem(typeof(string))]
    [XmlArrayItem(typeof(int))]
    [XmlArrayItem(typeof(double))]
    public object [] PrimitiveTypes = new object[]{"one", 2, 3.0};
 }

results in this XML:

 <?xml version="1.0" encoding="utf-8"?>
 <Arrays xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <PrimitiveTypes>
     <string>one</string>
     <int>2</int>
     <double>3</double>
   </PrimitiveTypes>
 </Arrays>

The following example serializes an array of objects. The field that returns the array is attributed with two XmlArrayItemAttribute instances. Each instance instructs the XmlSerializer to accept the specified Type in the array.

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

public class Group
{
   /* The Type property instructs the XmlSerializer to accept both
   the Person and Manager types in the array. */
   [XmlArrayItem(Type = typeof(Manager)),
   XmlArrayItem(Type=typeof(Person))]
   public Person[]Staff;
}

public class Person
{
   public string Name;
}

public class Manager:Person
{
   public int Rank;
}

public class Run 
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeOrder("TypeEx.xml");
   }


   public void SerializeOrder(string filename)
   {
      // Creates an XmlSerializer.
      XmlSerializer xSer = 
      new XmlSerializer(typeof(Group));

      // Creates the Group object, and two array items.
      Group myGroup = new Group();

      Person p1 = new Person();
      p1.Name = "Jacki";
      Manager p2 = new Manager();

      p2.Name = "Megan";
      p2.Rank = 2;

      Person [] myStaff = {p1,p2};
      myGroup.Staff = myStaff;

      // Serializes the object, and closes the StreamWriter.
      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myGroup);
   }
}
#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 Person
{
public:
   String* Name;
};

public __gc class Manager:public Person
{
public:
   int Rank;
};

public __gc class Group
{
   /* The Type property instructs the XmlSerializer to accept both
   the Person and Manager types in the array. */
public:
   [XmlArrayItem(Type = __typeof(Manager)),
      XmlArrayItem(Type=__typeof(Person))]
   Person* Staff[];
};

void SerializeOrder(String* filename)
{
   // Creates an XmlSerializer.
   XmlSerializer* xSer = 
      new XmlSerializer(__typeof(Group));

   // Creates the Group object, and two array items.
   Group* myGroup = new Group();

   Person* p1 = new Person();
   p1->Name = S"Jacki";
   Manager* p2 = new Manager();

   p2->Name = S"Megan";
   p2->Rank = 2;

   Person* myStaff[] = {p1,p2};
   myGroup->Staff = myStaff;

   // Serializes the object, and closes the StreamWriter.
   TextWriter* writer = new StreamWriter(filename);
   xSer->Serialize(writer, myGroup);
}

int main()
{
   SerializeOrder(S"TypeEx.xml");
}

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: