Export (0) Print
Expand All

XmlAttributes.XmlType Property

Gets or sets an object that specifies how the XmlSerializer serializes a class to which the XmlTypeAttribute has been applied.

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

'Declaration
Public Property XmlType As XmlTypeAttribute

Property Value

Type: System.Xml.Serialization.XmlTypeAttribute
An XmlTypeAttribute that overrides an XmlTypeAttribute applied to a class declaration.

The XmlTypeAttribute can be used to control how a type is serialized by the XmlSerializer. For example, by default, when a type is serialized, the XmlSerializer uses the class name as the XML element name. By creating an XmlTypeAttribute, setting the XmlType property to it, and creating an XmlAttributeOverrides object, you can change the XML element name.

The following example creates an XmlTypeAttribute object, and assigns it to the XmlType property of an XmlAttributes object.

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


Public Class Transportation
    Public Cars() As Car
End Class 

Public Class Car
    Public ID As Integer 
End Class 


Public Class Test

    Public Shared Sub Main()
        Dim t As New Test()
        t.SerializeObject("XmlType.xml")
    End Sub 


    ' Return an XmlSerializer used for overriding. 
    Public Function CreateOverrider() As XmlSerializer
        ' Create the XmlAttributes and XmlAttributeOverrides objects. 
        Dim attrs As New XmlAttributes()
        Dim xOver As New XmlAttributeOverrides()

        ' Create an XmlTypeAttribute and change the name of the 
        ' XML type.  
        Dim xType As New XmlTypeAttribute()
        xType.TypeName = "Autos" 

        ' Set the XmlTypeAttribute to the XmlType property.
        attrs.XmlType = xType

        ' Add the XmlAttributes to the XmlAttributeOverrides, 
        ' specifying the member to override. 
        xOver.Add(GetType(Car), attrs)

        ' Create the XmlSerializer, and return it. 
        Dim xSer As New XmlSerializer(GetType(Transportation), xOver)
        Return xSer
    End Function 


    Public Sub SerializeObject(ByVal filename As String)
        ' Create an XmlSerializer instance. 
        Dim xSer As XmlSerializer = CreateOverrider()

        ' Create an object and serialize it. 
        Dim myTransportation As New Transportation()

        Dim c1 As New Car()
        c1.ID = 12

        Dim c2 As New Car()
        c2.ID = 44

        myTransportation.Cars = New Car(1) {c1, c2}

        ' To write the file, a TextWriter is required. 
        Dim writer As New StreamWriter(filename)
        xSer.Serialize(writer, myTransportation)
    End Sub 
End Class

.NET Framework

Supported in: 4.5.2, 4.5.1, 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 8, Silverlight 8.1

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.

Show:
© 2014 Microsoft