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.
Assembly: System.Xml (in System.Xml.dll)
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
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.