This documentation is archived and is not being maintained.

XmlSerializer Constructor (Type, array<Type>)

Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and deserialize XML documents into object of a specified type. If a property or field returns an array, the extraTypes parameter specifies objects that can be inserted into the array.

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

public:
XmlSerializer(
	Type^ type, 
	array<Type^>^ extraTypes
)

Parameters

type
Type: System::Type

The type of the object that this XmlSerializer can serialize.

extraTypes
Type: array<System::Type>

A Type array of additional object types to serialize.

By default, if a public property or field returns an object, or array of objects, the object types are automatically serialized. However, if a class contains a field or property that returns an array of type Object, any object can be inserted into that array. In that case, the XmlSerializer must be instructed to expect all the possible object types that are inserted into the Object array. To do this, use the extraTypes parameter to specify the extra object types to serialize or deserialize.

You can also use the extraTypes parameter to specify types derived from a base class. For example, suppose a base class named Phone exists, and a class named InternationalPhone derives from it. Use the extraTypes parameter to specify the derived type as well.

The following example serializes an instance of a class that contains a public field that returns an array of objects. The extraTypes parameter of the XmlSerializer constructor specifies the types of the objects that can be serialized in 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;
ref class Address;
ref class Phone;

// This defines the object that will be serialized. 
public ref class Teacher
{
public:
   String^ Name;
   Teacher(){}

   /* Note that the Info field returns an array of objects.
         Any object can be added to the array by adding the
         object type to the array passed to the extraTypes argument. */

   [XmlArray(ElementName="ExtraInfo",IsNullable=true)]
   array<Object^>^Info;
   Phone^ PhoneInfo;
};


// This defines one of the extra types to be included. 
public ref class Address
{
public:
   String^ City;
   Address(){}

   Address( String^ city )
   {
      City = city;
   }
};

// Another extra type to include. 
public ref class Phone
{
public:
   String^ PhoneNumber;
   Phone(){}

   Phone( String^ phoneNumber )
   {
      PhoneNumber = phoneNumber;
   }
};

// Another type, derived from Phone 
public ref class InternationalPhone: public Phone
{
public:
   String^ CountryCode;
   InternationalPhone(){}

   InternationalPhone( String^ countryCode )
   {
      CountryCode = countryCode;
   }
};

public ref class Run
{
public:
   static void main()
   {
      Run^ test = gcnew Run;
      test->SerializeObject( "Teacher.xml" );
      test->DeserializeObject( "Teacher.xml" );
   }

private:
   void SerializeObject( String^ filename )
   {
      // Writing the file requires a TextWriter.
      TextWriter^ myStreamWriter = gcnew StreamWriter( filename );

      // Create a Type array. 
      array<Type^>^extraTypes = gcnew array<Type^>(3);
      extraTypes[ 0 ] = Address::typeid;
      extraTypes[ 1 ] = Phone::typeid;
      extraTypes[ 2 ] = InternationalPhone::typeid;

      // Create the XmlSerializer instance.
      XmlSerializer^ mySerializer = gcnew XmlSerializer( Teacher::typeid,extraTypes );
      Teacher^ teacher = gcnew Teacher;
      teacher->Name = "Mike";

      // Add extra types to the Teacher object 
      array<Object^>^info = gcnew array<Object^>(2);
      info[ 0 ] = gcnew Address( "Springville" );
      info[ 1 ] = gcnew Phone( "555-0100" );
      teacher->Info = info;
      teacher->PhoneInfo = gcnew InternationalPhone( "000" );
      mySerializer->Serialize( myStreamWriter, teacher );
      myStreamWriter->Close();
   }

   void DeserializeObject( String^ filename )
   {
      // Create a Type array. 
      array<Type^>^extraTypes = gcnew array<Type^>(3);
      extraTypes[ 0 ] = Address::typeid;
      extraTypes[ 1 ] = Phone::typeid;
      extraTypes[ 2 ] = InternationalPhone::typeid;

      // Create the XmlSerializer instance.
      XmlSerializer^ mySerializer = gcnew XmlSerializer( Teacher::typeid,extraTypes );

      // Reading a file requires a FileStream.
      FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
      Teacher^ teacher = dynamic_cast<Teacher^>(mySerializer->Deserialize( fs ));

      // Read the extra information.
      Address^ a = dynamic_cast<Address^>(teacher->Info[ 0 ]);
      Phone^ p = dynamic_cast<Phone^>(teacher->Info[ 1 ]);
      InternationalPhone^ Ip = dynamic_cast<InternationalPhone^>(teacher->PhoneInfo);
      Console::WriteLine( teacher->Name );
      Console::WriteLine( a->City );
      Console::WriteLine( p->PhoneNumber );
      Console::WriteLine( Ip->CountryCode );
   }
};

int main()
{
   Run::main();
}
#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 Address;
public __gc class Phone;

// This defines the object that will be serialized.
public __gc class Teacher
{  
public:
   String* Name;
   Teacher(){}
   /* Note that the Info field returns an array of objects.
      Any object can be added to the array by adding the
      object type to the array passed to the extraTypes argument. */
   [XmlArray (ElementName = S"ExtraInfo", IsNullable = true)]
   Object* Info[];
   Phone* PhoneInfo;
};

// This defines one of the extra types to be included.
public __gc class Address
{  
public:
   String* City;

   Address(){}
   Address(String* city)
   {
      City = city;
   }

};

// Another extra type to include.
public __gc class Phone
{
public:
   String* PhoneNumber;
   Phone(){}
   Phone(String* phoneNumber)
   {
      PhoneNumber = phoneNumber;
   }
};

// Another type, derived from Phone
public __gc class InternationalPhone: public Phone
{
public:
   String* CountryCode;

   InternationalPhone(){}

   InternationalPhone(String* countryCode)
   {
      CountryCode = countryCode;
   }
};

public __gc class Run
{
public:
   static void main()
   {
      Run* test = new Run();
      test->SerializeObject(S"Teacher.xml");
      test->DeserializeObject(S"Teacher.xml");
   }

private:
   void SerializeObject(String* filename)
   {
      // Writing the file requires a TextWriter.
      TextWriter* myStreamWriter = new StreamWriter(filename);

      // Create a Type array.
      Type* extraTypes[]= new Type*[3];
      extraTypes[0] = __typeof(Address);
      extraTypes[1] = __typeof(Phone);
      extraTypes[2] = __typeof(InternationalPhone);

      // Create the XmlSerializer instance.
      XmlSerializer* mySerializer = new XmlSerializer
      (__typeof(Teacher),extraTypes);

      Teacher* teacher = new Teacher();
      teacher->Name = S"Mike";
      // Add extra types to the Teacher object
      Object* info[] = new Object*[2];
      info[0] = new Address(S"Springville");
      info[1] = new Phone(S"555-0100");

      teacher->Info = info;

      teacher->PhoneInfo = new InternationalPhone(S"000"); 

      mySerializer->Serialize(myStreamWriter,teacher);
      myStreamWriter->Close();
   }

   void DeserializeObject(String* filename)
   {
      // Create a Type array.
      Type* extraTypes[]= new Type*[3];
      extraTypes[0] = __typeof(Address);
      extraTypes[1] = __typeof(Phone);
      extraTypes[2] = __typeof(InternationalPhone);

      // Create the XmlSerializer instance.
      XmlSerializer* mySerializer = new XmlSerializer
      (__typeof(Teacher),extraTypes);

      // Reading a file requires a FileStream.
      FileStream* fs = new FileStream(filename, FileMode::Open);
      Teacher* teacher = dynamic_cast<Teacher*> (mySerializer->Deserialize(fs));

      // Read the extra information.
      Address* a = dynamic_cast<Address*>(teacher->Info[0]);
      Phone* p = dynamic_cast<Phone*> (teacher->Info[1]);
      InternationalPhone* Ip = 
      dynamic_cast<InternationalPhone*> (teacher->PhoneInfo);

      Console::WriteLine(teacher->Name);
      Console::WriteLine(a->City);
      Console::WriteLine(p->PhoneNumber);
      Console::WriteLine(Ip->CountryCode);
   }
};

int main()
{
   Run::main();
}

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

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: