This documentation is archived and is not being maintained.

XmlSerializer.XmlSerializer(Type, Type[]) Constructor

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)

'Declaration
Public Sub New ( _
	type As Type, _
	extraTypes As Type() _
)
'Usage
Dim type As Type
Dim extraTypes As Type()

Dim instance As New XmlSerializer(type, extraTypes)
public XmlSerializer (
	Type type, 
	Type[] extraTypes
)
public function XmlSerializer (
	type : Type, 
	extraTypes : Type[]
)
Not applicable.

Parameters

type

The type of the object that this XmlSerializer can serialize.

extraTypes

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.

Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization



' This defines the object that will be serialized.
Public Class Teacher
    Public Name As String
    
    Public Sub New()
    End Sub 'New 
    ' 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 Info() As Object
    Public PhoneInfo As Phone
End Class 'Teacher


' This defines one of the extra types to be included.
Public Class Address
    Public City As String
    
    Public Sub New()
    End Sub 'New
    
    Public Sub New(city As String)
        me.City = city
    End Sub 'New
End Class 'Address
 

' Another extra type to include.
Public Class Phone
    Public PhoneNumber As String
    
    Public Sub New()
    End Sub 'New
    
    Public Sub New(phoneNumber As String)
        me.PhoneNumber = phoneNumber
    End Sub 'New
End Class 'Phone


' Another type, derived from Phone.
Public Class InternationalPhone
    Inherits Phone
    Public CountryCode As String
    
    
    Public Sub New()
    End Sub 'New
     
    Public Sub New(countryCode As String)
        me.CountryCode = countryCode
    End Sub 'New
End Class 'InternationalPhone


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("Teacher.xml")
        test.DeserializeObject("Teacher.xml")
    End Sub 'Main
    
    
    Private Sub SerializeObject(filename As String)
        ' Writing the file requires a TextWriter.
        Dim myStreamWriter As New StreamWriter(filename)
        
        ' Create a Type array.
        Dim extraTypes(2) As Type
        extraTypes(0) = GetType(Address)
        extraTypes(1) = GetType(Phone)
        extraTypes(2) = GetType(InternationalPhone)
        
        ' Create the XmlSerializer instance.
        Dim mySerializer As New XmlSerializer(GetType(Teacher), extraTypes)
        
        Dim teacher As New Teacher()
        teacher.Name = "Mike"
        ' Add extra types to the Teacher object.
        Dim info(1) As Object
        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()
    End Sub 'SerializeObject
    
    
    Private Sub DeserializeObject(filename As String)
        ' Create a Type array.
        Dim extraTypes(2) As Type
        extraTypes(0) = GetType(Address)
        extraTypes(1) = GetType(Phone)
        extraTypes(2) = GetType(InternationalPhone)
        
        ' Create the XmlSerializer instance.
        Dim mySerializer As New XmlSerializer(GetType(Teacher), extraTypes)
        
        ' Reading a file requires a FileStream.
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim teacher As Teacher = CType(mySerializer.Deserialize(fs), Teacher)
        
        ' Read the extra information.
        Dim a As Address = CType(teacher.Info(0), Address)
        Dim p As Phone = CType(teacher.Info(1), Phone)
        Dim Ip As InternationalPhone = CType(teacher.PhoneInfo, InternationalPhone)
        
        Console.WriteLine(teacher.Name)
        Console.WriteLine(a.City)
        Console.WriteLine(p.PhoneNumber)
        Console.WriteLine(Ip.CountryCode)
    End Sub 'DeserializeObject
End Class 'Run

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

// This defines the object that will be serialized.
public class Teacher
{
    public String name;

    public Teacher()
    {
    } //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. */
    /** @attribute XmlArray(ElementName = "ExtraInfo", IsNullable = true)
     */
    public Object info[];
    public Phone phoneInfo;
} //Teacher

// This defines one of the extra types to be included.
public class Address
{
    public String city;
    public Address()
    {
    } //Address
    public Address(String tCity)
    {
        city = tCity;
    } //Address
} //Address 

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

// Another type, derived from Phone
public class InternationalPhone extends Phone
{
    public String countryCode;
    public InternationalPhone()
    {
    } //InternationalPhone
    public InternationalPhone(String tCountryCode)
    {
        countryCode = tCountryCode;
    } //InternationalPhone
} //InternationalPhone

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

    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.set_Item(0, Address.class.ToType());
        extraTypes.set_Item(1, Phone.class.ToType());
        extraTypes.set_Item(2, InternationalPhone.class.ToType());

        // Create the XmlSerializer instance.
        XmlSerializer mySerializer =
            new XmlSerializer(Teacher.class.ToType(), extraTypes);
        Teacher teacher = new Teacher();

        teacher.name = "Mike";

        // Add extra types to the Teacher object
        Object info[] = new Object[2];

        info.set_Item(0, new Address("Springville"));
        info.set_Item(1, new Phone("000-0000"));
        teacher.info = info;
        teacher.phoneInfo = new InternationalPhone("000");
        mySerializer.Serialize(myStreamWriter, teacher);
        myStreamWriter.Close();
    } //SerializeObject


    private void DeserializeObject(String filename)
    {
        // Create a Type array.
        Type extraTypes[] = new Type[3];

        extraTypes.set_Item(0, Address.class.ToType());
        extraTypes.set_Item(1, Phone.class.ToType());
        extraTypes.set_Item(2, InternationalPhone.class.ToType());

        // Create the XmlSerializer instance.
        XmlSerializer mySerializer =
            new XmlSerializer(Teacher.class.ToType(), 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);
    } //DeserializeObject
} //Run

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0
Show: