XmlArrayAttribute.ElementName Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets the XML element name given to the serialized array.

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

Public Property ElementName As String

Property Value

Type: System.String

The XML element name of the serialized array. The default is the name of the member to which the XmlArrayAttribute is assigned.

Specify an ElementName when you want the generated XML element name to differ from the member's identifier.

You can set the same ElementName value to more than one member as long as the generated XML document uses XML namespaces to distinguish between the identically named members. For more details about using namespaces and creating prefixed names in the XML document, see XmlSerializerNamespaces.

The following example serializes an instance of the Library class that contains a property named Books that returns an array of Book items. The example uses the ElementName property to specify that the array of XML elements should be named My_Books rather than Books.

Option Explicit
Option Strict

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

Public Class Library
    Private myBooks() As Book

    <XmlArray(ElementName := "My_Books")> _
    Public Property Books() As Book()
            Return myBooks
        End Get
            myBooks = value
        End Set
    End Property
End Class

Public Class Book
    Public Title As String
    Public Author As String
    Public ISBN As String
End Class

Public Class Run

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

    Public Sub WriteBook(ByVal filename As String)
        Dim mySerializer As New XmlSerializer(GetType(Library))
        Dim t As New StreamWriter(filename)
        Dim ns As New XmlSerializerNamespaces()
        ns.Add("bk", "http://wwww.contoso.com")

        Dim b1 As New Book()
        b1.Title = "MyBook Title"
        b1.Author = "An Author"
        b1.ISBN = "00000000"

        Dim b2 As New Book()
        b2.Title = "Another Title"
        b2.Author = "Another Author"
        b2.ISBN = "0000000"

        Dim myLibrary As New Library()
        Dim myBooks() As Book =  {b1, b2}
        myLibrary.Books = myBooks

        mySerializer.Serialize(t, myLibrary, ns)
    End Sub
End Class

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top