Export (0) Print
Expand All

XmlArrayItemAttribute Constructor

Initializes a new instance of the XmlArrayItemAttribute class.

Namespace:  System.Xml.Serialization
Assemblies:   System.Xml.XmlSerializer (in System.Xml.XmlSerializer.dll)
  System.Xml (in System.Xml.dll)


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.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public ref class Vehicle
   String^ id;

public ref class Car: public Vehicle
   String^ Maker;

public ref class Transportation


void SerializeObject( String^ filename )
   // Creates an XmlSerializer for the Transportation class. 
   XmlSerializer^ MySerializer = gcnew XmlSerializer( Transportation::typeid );

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

   // Creates the object to serialize.
   Transportation^ myTransportation = gcnew Transportation;

   // Creates objects to add to the array.
   Vehicle^ myVehicle = gcnew Vehicle;
   myVehicle->id = "A12345";
   Car^ myCar = gcnew Car;
   myCar->id = "Car 34";
   myCar->Maker = "FamousCarMaker";
   array<Vehicle^>^temp = {myVehicle,myCar};
   myTransportation->MyVehicles = temp;

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

void DeserializeObject( String^ filename )
   // Creates an XmlSerializer instance.
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Transportation::typeid );
   FileStream^ myFileStream = gcnew 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( "XmlArrayItem1.xml" );
   DeserializeObject( "XmlArrayItem1.xml" );

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

© 2014 Microsoft