ElementName Property
Collapse the table of content
Expand the table of content

XmlArrayAttribute.ElementName Property


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

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

public string ElementName { get; set; }

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.

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", "http://wwww.contoso.com");

      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;


Universal Windows Platform
Available since 4.5
.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
© 2015 Microsoft