Export (0) Print
Expand All

XmlAttributeAttribute.AttributeName Property

Gets or sets the name of the XML attribute.

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

'Declaration
Public Property AttributeName As String

Property Value

Type: System.String
The name of the XML attribute. The default is the member name.

Use the AttributeName property to specify an XML attribute name when the default value cannot be used. For example, if the XML attribute name is invalid as a member identifier, you can use a valid name for the identifier while setting the AttributeName to an invalid name.

The following example sets the AttributeName property of an XmlAttributeAttribute.

Option Explicit
Option Strict

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


Public Class Group
    ' Change the XML attribute name.
    <XmlAttribute(AttributeName := "MgrName")> Public Name As String 
    ' Use the AttributeName to collect all the XML attributes 
    ' in the XML-document instance. 
    <XmlAttribute(AttributeName := "*")> Public Attrs() As XmlAttribute
End Class 

Public Class Run

    Public Shared Sub Main()
        Dim test As New Run()
        ' To use the AttributeName to collect all the 
        ' XML attributes. Call SerializeObject to generate 
        ' an XML document and alter the document by adding 
        ' new XML attributes to it. Then comment out the SerializeObject 
        ' method, and call DeserializeObject.
        test.SerializeObject("MyAtts.xml")
        test.DeserializeObject("MyAtts.xml")
    End Sub 

    Public Sub SerializeObject(ByVal filename As String)
        Console.WriteLine("Serializing")
        ' Create an instance of the XmlSerializer class. 
        Dim mySerializer As New XmlSerializer(GetType(Group))
        ' 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.Name = "Wallace" 
        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup)
        writer.Close()
    End Sub 

    Public Sub DeserializeObject(ByVal filename As String)
        Console.WriteLine("Deserializing")
        Dim mySerializer As New XmlSerializer(GetType(Group))
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)
        ' The following code prints all the attributes in the 
        ' XML document. To collect the attributes, the AttributeName of the 
        ' XmlAttributeAttribute must be set to "*". 
        Dim xAtt As XmlAttribute
        For Each xAtt In  myGroup.Attrs
            Console.WriteLine(xAtt.Name & ":" & xAtt.Value)
        Next xAtt
    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