Export (0) Print
Expand All

XmlAttributes.XmlRoot Property

Gets or sets an object that specifies how the XmlSerializer serializes a class as an XML root element.

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

'Declaration
Public Property XmlRoot As XmlRootAttribute

Property Value

Type: System.Xml.Serialization.XmlRootAttribute
An XmlRootAttribute that overrides a class attributed as an XML root element.

The following example creates an XmlAttributeOverrides object, an XmlAttributes object, and an XmlRootAttribute object. The example assigns the XmlRootAttribute to the XmlRoot property of the XmlAttributes object, and adds the XmlAttributes object to the XmlAttributeOverrides object. Lastly, the example gets the XmlAttributes object by passing the Type of the serialized class to the XmlAttributeOverrides object. (In this example, the Type is Group.)

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


' This is the class that will be serialized. 
Public Class Group
    Public GroupName As String
    <XmlAttribute()> Public GroupCode As Integer 
End Class 


Public Class Test

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


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

        Dim xRoot As New XmlRootAttribute()

        ' Set a new Namespace and ElementName for the root element.
        xRoot.Namespace = "http://www.cpandl.com"
        xRoot.ElementName = "NewGroup"
        attrs.XmlRoot = xRoot

        ' Add the XmlAttributes object to the XmlAttributeOverrides. 
        ' No  member name is needed because the whole class is 
        ' overridden. 
        xOver.Add(GetType(Group), attrs)

        ' Get the XmlAttributes object, based on the type. 
        Dim tempAttrs As XmlAttributes
        tempAttrs = xOver(GetType(Group))

        ' Print the Namespace and ElementName of the root.
        Console.WriteLine(tempAttrs.XmlRoot.Namespace)
        Console.WriteLine(tempAttrs.XmlRoot.ElementName)

        Dim xSer As New XmlSerializer(GetType(Group), xOver)
        Return xSer
    End Function 


    Public Sub SerializeObject(ByVal filename As String)
        ' Create the XmlSerializer using the CreateOverrider method. 
        Dim xSer As XmlSerializer = CreateOverrider()

        ' Create the object to serialize. 
        Dim myGroup As New Group()
        myGroup.GroupName = ".NET"
        myGroup.GroupCode = 123

        ' To write the file, a TextWriter is required. 
        Dim writer As New StreamWriter(filename)

        ' Serialize the object and close the TextWriter.
        xSer.Serialize(writer, myGroup)
        writer.Close()
    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