Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

XmlSerializer Constructor (Type, 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,
	Type[] extraTypes
)

Parameters

type
Type: System.Type

The type of the object that this XmlSerializer can serialize.

extraTypes
Type: 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;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This defines the object that will be serialized. 
public class Teacher
{  
   public string Name;
   public 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)]
   public object[] Info;
   public Phone PhoneInfo;
}

// This defines one of the extra types to be included. 
public class Address
{  
   public string City;

   public Address(){}
   public Address(string city)
   {
      City = city;
   }

}

// Another extra type to include. 
public class Phone
{
   public string PhoneNumber;
   public Phone(){}
   public Phone(string phoneNumber)
   {
      PhoneNumber = phoneNumber;
   }
}

// Another type, derived from Phone 
public class InternationalPhone:Phone
{
   public string CountryCode;

   public InternationalPhone(){}

   public InternationalPhone(string countryCode)
   {
      CountryCode = countryCode;
   }
}

public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("Teacher.xml");
      test.DeserializeObject("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 = "Mike";
      // Add extra types to the Teacher object
      object [] info = new object[2];
      info[0] = new Address("Springville");
      info[1] = new Phone("555-0100");

      teacher.Info = info;

      teacher.PhoneInfo = new InternationalPhone("000"); 

      mySerializer.Serialize(myStreamWriter,teacher);
      myStreamWriter.Close();
   }

   private 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 = (Teacher) mySerializer.Deserialize(fs);

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

      Console.WriteLine(teacher.Name);
      Console.WriteLine(a.City);
      Console.WriteLine(p.PhoneNumber);
      Console.WriteLine(Ip.CountryCode);
   }
}
#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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.