This documentation is archived and is not being maintained.

XmlArrayAttribute.ElementName Property

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

[Visual Basic]
Public Property ElementName As String
public string ElementName {get; set;}
public: __property String* get_ElementName();
public: __property void set_ElementName(String*);
public function get ElementName() : String;
public function set ElementName(String);

Property Value

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 on using namespaces and creating prefixed names in the XML document, see XmlSerializerNamespaces.


[Visual Basic, C#, C++] 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.

[Visual Basic] 
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", "")
        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

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Library
   private Book[] books;
   public Book[] Books
      get{return books;}
      set{books = value;}
public class Book
   public string Title;
   public string Author;
   public string ISBN;
public class Run
   public static void Main()
      Run test = new Run();
   public void WriteBook(string filename)
      XmlSerializer mySerializer = new XmlSerializer(typeof(Library));
      TextWriter t = new StreamWriter(filename);
      XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
      ns.Add("bk", "");
      Book b1 = new Book();
      b1.Title = "MyBook Title";
      b1.Author = "An Author";
      b1.ISBN = "00000000";
      Book b2 = new Book();
      b2.Title = "Another Title";
      b2.Author = "Another Author";
      b2.ISBN = "0000000";
      Library myLibrary = new Library();
      Book[] myBooks = {b1,b2};
      myLibrary.Books = myBooks;

#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;

public __gc class Book
   String* Title;
   String* Author;
   String* ISBN;

public __gc class Library
   Book* books[];
   __property Book*get_Books()[]{return books;}
   __property void set_Books( Book* value[] ){books = value;}


int main()
   String* filename = S"ArrayExample.xml";
   XmlSerializer* mySerializer = new XmlSerializer(__typeof(Library));
   TextWriter* t = new StreamWriter(filename);
   XmlSerializerNamespaces* ns = new XmlSerializerNamespaces();
   ns->Add(S"bk", S"");

   Book* b1 = new Book();
   b1->Title = S"MyBook Title";
   b1->Author = S"An Author";
   b1->ISBN = S"00000000";

   Book* b2 = new Book();
   b2->Title = S"Another Title";
   b2->Author = S"Another Author";
   b2->ISBN = S"0000000";

   Library* myLibrary = new Library();
   Book* myBooks[] = {b1,b2};
   myLibrary->Books = myBooks;


[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

XmlArrayAttribute Class | XmlArrayAttribute Members | System.Xml.Serialization Namespace | XmlSerializer | Namespace | Form