Export (0) Print
Expand All

XmlSerializer Constructor (Type, XmlAttributeOverrides, Type(), XmlRootAttribute, String)

Initializes a new instance of the XmlSerializer class that can serialize objects of type Object into XML document instances, and deserialize XML document instances into objects of type Object. Each object to be serialized can itself contain instances of classes, which this overload overrides with other classes. This overload also specifies the default namespace for all the XML elements and the class to use as the XML root element.

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

Public Sub New ( _
	type As Type, _
	overrides As XmlAttributeOverrides, _
	extraTypes As Type(), _
	root As XmlRootAttribute, _
	defaultNamespace As String _


Type: System.Type

The type of the object that this XmlSerializer can serialize.

Type: System.Xml.Serialization.XmlAttributeOverrides

An XmlAttributeOverrides that extends or overrides the behavior of the class specified in the type parameter.

Type: System.Type()

A Type array of additional object types to serialize.

Type: System.Xml.Serialization.XmlRootAttribute

An XmlRootAttribute that defines the XML root element properties.

Type: System.String

The default namespace of all XML elements in the XML document.

The overrides parameter allows for the creation of an XmlSerializer that serializes a class that extends or overrides the behavior of a base class. For example, given a DLL, it is possible to create a class that inherits or extends a class contained in the DLL. To serialize such a class, you must use an instance of the XmlAttributeOverrides class when constructing the XmlSerializer. For more details, see XmlAttributeOverrides.

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.

The root element of an XML document encloses all the other elements. By default, the object specified by the type parameter is serialized as the root element. Properties, such as the XML element name of the root element are taken from the type object. However, the root parameter allows you to replace the default object's information by specifying an XmlRootAttribute; the object allows you to set a different namespace, element name, and so on.

Use the defaultName parameter to specify the default namespace of all XML elements generated by the XmlSerializer.

The following example serializes an instance of a class that is defined in a DLL and to do so, overrides the public members found in the class. The example also specifies an array of extra types, the default namespace for all XML elements, and the class to use that provides the XML root element information. The example assumes that the code at the beginning has been compiled into a DLL named HighSchool.

'Beginning of the HighSchool.dll  
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Imports Microsoft.VisualBasic
' Imports HighSchool 

Namespace HighSchool

    Public Class Student
        Public Name As String 
        Public ID As Integer 
    End Class 'Student

    Public Class ClassRoom
        Public Students() As Student
    End Class 'ClassRoom
End Namespace 'HighSchool

Namespace College
    Public Class Graduate
        Inherits HighSchool.Student

        Public Sub New()
        End Sub 'New 
        ' Add a new field named University. 
        Public University As String 
        ' Use extra types to use this field. 
        Public Info() As Object 
    End Class 'Graduate

    Public Class Address
        Public City As String 
    End Class 'Address

    Public Class Phone
        Public Number As String 
    End Class 'Phone

    Public Class Run

        Public Shared Sub Main()
            Dim test As New Run()
        End Sub 'Main

        Private Sub WriteOverriddenAttributes(ByVal filename As String)
            ' Writing the file requires a TextWriter. 
            Dim myStreamWriter As New StreamWriter(filename)
            ' Create an XMLAttributeOverrides class. 
            Dim attrOverrides As New XmlAttributeOverrides()
            ' Create the XmlAttributes class. 
            Dim attrs As New XmlAttributes()
            ' Override the Student class. "Alumni" is the name 
            ' of the overriding element in the XML output.  

            Dim attr As New XmlElementAttribute("Alumni", GetType(Graduate))
            ' Add the XmlElementAttribute to the collection of 
            ' elements in the XmlAttributes object. 
            ' Add the XmlAttributes to the XmlAttributeOverrides. 
            ' "Students" is the name being overridden. 
            attrOverrides.Add(GetType(HighSchool.ClassRoom), "Students", attrs)

            ' Create array of extra types. 
            Dim extraTypes(1) As Type
            extraTypes(0) = GetType(Address)
            extraTypes(1) = GetType(Phone)

            ' Create an XmlRootAttribute. 
            Dim root As New XmlRootAttribute("Graduates")

            ' Create the XmlSerializer with the 
            ' XmlAttributeOverrides object.  
            Dim mySerializer As New XmlSerializer(GetType(HighSchool.ClassRoom), _
                attrOverrides, extraTypes, root, "http://www.microsoft.com")

            Dim oMyClass As New HighSchool.ClassRoom()

            Dim g1 As New Graduate()
            g1.Name = "Jacki"
            g1.ID = 1
            g1.University = "Alma" 

            Dim g2 As New Graduate()
            g2.Name = "Megan"
            g2.ID = 2
            g2.University = "CM" 

            Dim myArray As HighSchool.Student() = {g1, g2}

            oMyClass.Students = myArray

            ' Create extra information. 
            Dim a1 As New Address()
            a1.City = "Ionia" 
            Dim a2 As New Address()
            a2.City = "Stamford" 
            Dim p1 As New Phone()
            p1.Number = "555-0101" 
            Dim p2 As New Phone()
            p2.Number = "555-0100" 

            Dim o1() As Object = {a1, p1}
            Dim o2() As Object = {a2, p2}

            g1.Info = o1
            g2.Info = o2
            mySerializer.Serialize(myStreamWriter, oMyClass)
        End Sub 'WriteOverriddenAttributes

        Private Sub ReadOverriddenAttributes(ByVal filename As String)
            ' The majority of the code here is the same as that in the 
            ' WriteOverriddenAttributes method. Because the XML being read 
            ' doesn't conform to the schema defined by the DLL, the 
            ' XMLAttributesOverrides must be used to create an 
            ' XmlSerializer instance to read the XML document. 

            Dim attrOverrides As New XmlAttributeOverrides()
            Dim attrs As New XmlAttributes()
            Dim attr As New XmlElementAttribute("Alumni", GetType(Graduate))
            attrOverrides.Add(GetType(HighSchool.ClassRoom), "Students", attrs)

            Dim extraTypes(1) As Type
            extraTypes(0) = GetType(Address)
            extraTypes(1) = GetType(Phone)

            Dim root As New XmlRootAttribute("Graduates")

            Dim readSerializer As New XmlSerializer(GetType(HighSchool.ClassRoom), _
                attrOverrides, extraTypes, root, "http://www.microsoft.com")

            ' A FileStream object is required to read the file.  
            Dim fs As New FileStream(filename, FileMode.Open)

            Dim oMyClass As HighSchool.ClassRoom
            oMyClass = CType(readSerializer.Deserialize(fs), HighSchool.ClassRoom)

            ' Here is the difference between reading and writing an 
            ' XML document: You must declare an object of the derived 
            ' type (Graduate) and cast the Student instance to it. 
            Dim g As Graduate
            Dim a As Address
            Dim p As Phone
            Dim grad As Graduate
            For Each grad In oMyClass.Students
                g = CType(grad, Graduate)
                Console.Write((g.Name & ControlChars.Tab))
                Console.Write((g.ID & ControlChars.Tab))
                Console.Write((g.University & ControlChars.Cr))
                a = CType(g.Info(0), Address)
                p = CType(g.Info(1), Phone)
            Next grad
        End Sub 'ReadOverriddenAttributes
    End Class 'Run
End Namespace 'College

.NET Framework

Supported in: 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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, 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