Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
XmlArrayItemAttribute Constructor ()

XmlArrayItemAttribute Constructor ()

Initializes a new instance of the XmlArrayItemAttribute class.

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

public XmlArrayItemAttribute ()
public XmlArrayItemAttribute ()
public function XmlArrayItemAttribute ()

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.

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(), 
   XmlArrayItem(typeof(Car), ElementName = "Automobile")]
   public Vehicle[] MyVehicles;
}
 
public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("XmlArrayItem1.xml");
      test.DeserializeObject("XmlArrayItem1.xml");
   }

   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);

      // Creates the object to serialize.
      Transportation myTransportation = new Transportation();

      // Creates objects to add to the array.
      Vehicle myVehicle= new Vehicle() ;
      myVehicle.id = "A12345";

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

      myTransportation.MyVehicles = 
      new Vehicle[2] {myVehicle, myCar};
      
      // Serializes the object, and closes the StreamWriter.
      MySerializer.Serialize(myTextWriter, myTransportation);
      myTextWriter.Close();
   }
 
   private void DeserializeObject(string filename)
   {
      // Creates an XmlSerializer instance.
      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++)
      {
         Console.WriteLine(myTransportation.MyVehicles[i].id);
      }
   }
 }

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

public class Vehicle
{
    public String id;
} //Vehicle

public class Car extends Vehicle
{
    public String maker;
} //Car

public class Transportation
{
    /** @attribute XmlArrayItem()
        @attribute XmlArrayItem(Car.class, ElementName = "Automobile")
     */
    public Vehicle myVehicles[];
} //Transportation

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();
        test.SerializeObject("XmlArrayItem1.xml");
        test.DeserializeObject("XmlArrayItem1.xml");
    } //main

    private void SerializeObject(String fileName)
    {
        // Creates an XmlSerializer for the Transportation class. 
        XmlSerializer mySerializer =
            new XmlSerializer(Transportation.class.ToType());

        // Writing the XML file to disk requires a TextWriter.
        TextWriter myTextWriter = new StreamWriter(fileName);
        
        // Creates the object to serialize.
        Transportation myTransportation = new Transportation();

        // Creates objects to add to the array.
        Vehicle myVehicle = new Vehicle();
        myVehicle.id = "A12345";

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

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

        // Serializes the object, and closes the StreamWriter.
        mySerializer.Serialize(myTextWriter, myTransportation);
        myTextWriter.Close();
    } //SerializeObject

    private void DeserializeObject(String fileName)
    {
        // Creates an XmlSerializer instance.
        XmlSerializer mySerializer =
            new XmlSerializer(Transportation.class.ToType());
        FileStream myFileStream = new FileStream(fileName, FileMode.Open);
        Transportation myTransportation =
            (Transportation)mySerializer.Deserialize(myFileStream);

        for (int i = 0; i < myTransportation.myVehicles.length; i++) {
            Console.WriteLine(myTransportation.myVehicles[i].id);
        }
    } //DeserializeObject
} //Run

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft