Export (0) Print
Expand All

XmlArrayItemAttribute Constructor

Initializes a new instance of the XmlArrayItemAttribute class.

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

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

public __gc class Vehicle
{
public:
   String* id;
};
public __gc class Car:public Vehicle
{
public:
   String* Maker;   
};

public __gc class Transportation
{  
public:
   [XmlArrayItem, 
      XmlArrayItem(__typeof(Car), ElementName = S"Automobile")]
   Vehicle* MyVehicles[];
};

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 = S"A12345";

   Car* myCar = new Car();
   myCar->id = S"Car 34";
   myCar->Maker = S"FamousCarMaker";

   Vehicle* temp[] = {myVehicle, myCar};
   myTransportation->MyVehicles = temp;

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

void DeserializeObject(String* filename)
{
   // Creates an XmlSerializer instance.
   XmlSerializer* mySerializer = new XmlSerializer(__typeof(Transportation));
   FileStream* myFileStream = new FileStream(filename,FileMode::Open);
   Transportation* myTransportation =
      dynamic_cast<Transportation*> (mySerializer->Deserialize(myFileStream));

   for(int i = 0; i < myTransportation->MyVehicles->Length;i++)
   {
      Console::WriteLine(myTransportation->MyVehicles[i]->id);
   }
}

int main()
{
   SerializeObject(S"XmlArrayItem1.xml");
   DeserializeObject(S"XmlArrayItem1.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

Community Additions

ADD
Show:
© 2014 Microsoft