Export (0) Print
Expand All

XmlAttributes.XmlAttribute Property

Gets or sets an object that specifies how the XmlSerializer serializes a public field or public read/write property as an XML attribute.

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

'Declaration
Public Property XmlAttribute As XmlAttributeAttribute

Property Value

Type: System.Xml.Serialization.XmlAttributeAttribute
An XmlAttributeAttribute that controls the serialization of a public field or read/write property as an XML attribute.

By default, if no attribute is applied to a public field or public read/write property, it is serialized as an XML element. You can also instruct the XmlSerializer to generate an XML attribute by applying an XmlAttributeAttribute to the field or property.

The XmlAttribute property allows you to override the default serialization, as well as the serialization controlled by applying an XmlAttributeAttribute to the member. To do this, create an XmlAttributeAttribute and set its properties (such as AttributeName). Assign the new object to the XmlAttribute property of an XmlAttributes object. Add the XmlAttributes object to an XmlAttributeOverrides object and specify the type of the object that contains the field or property, and the name of the field or property. Lastly, create an XmlSerializer using the XmlAttributeOverrides object and call the Serialize or Deserialize method.

The following example serializes a class named Group that contains a property named GroupName; the GroupName property is serialized as an XML attribute. The example creates an XmlAttributeOverrides and an XmlAttributes object to override the default serialization of the field. The example then creates an XmlAttributeAttribute to specifically override the property, and the object is set to the XmlAttribute property The XmlAttributes object is added to the XmlAttributeOverrides object with the name of the overridden member specified. Finally, an XmlSerializer is constructed and returned using the XmlAttributeOverrides object.

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


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


Public Class Run

    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("OverrideAttribute.xml")
        test.DeserializeObject("OverrideAttribute.xml")
    End Sub 

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

        ' Create an overriding XmlAttributeAttribute set it to 
        ' the XmlAttribute property of the XmlAttributes object. 
        Dim xAttribute As New XmlAttributeAttribute("SplinterName")
        xAttrs.XmlAttribute = xAttribute

        ' Add to the XmlAttributeOverrides. Specify the member name.
        xOver.Add(GetType(Group), "GroupName", xAttrs)

        ' Create the XmlSerializer and return it. 
        Return New XmlSerializer(GetType(Group), xOver)
    End Function 'CreateOverrider


    Public Sub SerializeObject(ByVal filename As String)
        ' Create an instance of the XmlSerializer class. 
        Dim mySerializer As XmlSerializer = CreateOverrider()
        ' Writing the file requires a TextWriter. 
        Dim writer As New StreamWriter(filename)

        ' Create an instance of the class that will be serialized. 
        Dim myGroup As New Group()

        ' Set the Name property, which will be generated 
        ' as an XML attribute. 
        myGroup.GroupName = ".NET"
        myGroup.GroupNumber = 1
        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup)
        writer.Close()
    End Sub 


    Public Sub DeserializeObject(ByVal filename As String)
        Dim mySerializer As XmlSerializer = CreateOverrider()
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)

        Console.WriteLine(myGroup.GroupName)
        Console.WriteLine(myGroup.GroupNumber)
    End Sub 
End Class

.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.

Show:
© 2014 Microsoft