XmlArrayItemAttribute Constructor (String, Type)


Initializes a new instance of the XmlArrayItemAttribute class and specifies the name of the XML element generated in the XML document and the Type that can be inserted into the generated XML document.

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

public XmlArrayItemAttribute(
	string elementName,
	Type type


Type: System.String

The name of the XML element.

Type: System.Type

The Type of the object to serialize.

This overload sets the ElementName and the Type properties.

Use this overload if you want the name of the generated XML element to differ from the member's identifier.

An XML document that includes namespaces can contain more than one version of an element name. For details, see the ElementName property.

The following example serializes a class named Transportation that contains a field named MyVehicles that returns an array of Vehicle objects. The example applies the XmlArrayItemAttribute to the field, allowing the XmlSerializer to insert instances of the Car class, which is derived from the Vehicle class, into the array. While applying the attribute, the example sets the ElementName property using the elementName parameter, and the Type property using the type parameter.

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

public class Vehicle
   public string id;
public class Car:Vehicle
   public string Maker;

public class Transportation
   [XmlArrayItem("Transport", typeof(Vehicle)), 
   XmlArrayItem("Automobile", typeof(Car))]
   public Vehicle[] MyVehicles;

public class Run
   public static void Main()
      Run test= new Run();

   private void SerializeObject(string filename)
       // Creates an XmlSerializer for the Transportation class.
      XmlSerializer MySerializer = 
      new XmlSerializer(typeof(Transportation));

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

      Transportation myTransportation = new Transportation();

      Vehicle myVehicle= new Vehicle() ;
      myVehicle.id = "A12345";

      Car myCar = new Car();
      myCar.id = "Car 34";
      myCar.Maker = "FamousCarMaker";

      Vehicle [] myVehicles = {myVehicle, myCar};
      myTransportation.MyVehicles = myVehicles;

      // Serializes the object, and closes the StreamWriter.
      MySerializer.Serialize(myTextWriter, myTransportation);

   private void DeserializeObject(string filename)
      // Creates an XmlSerializer.
      XmlSerializer mySerializer = 
      new XmlSerializer(typeof(Transportation));
      FileStream myFileStream = new FileStream(filename,FileMode.Open);
      Transportation myTransportation =
      (Transportation) mySerializer.Deserialize(myFileStream);

      for(int i = 0;i < myTransportation.MyVehicles.Length;i++)

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top