Export (0) Print
Expand All

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 */
public String get_ElementName ()

/** @property */
public void set_ElementName (String value)

public function get ElementName () : String

public function set ElementName (value : 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 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;
   [XmlArray(ElementName="My_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();
      test.WriteBook("ArrayExample.xml");
   }
 
   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;
       
      mySerializer.Serialize(t,myLibrary,ns);
      t.Close();
   }
}
   

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

public class Library
{
    private Book books[];

    /** @attribute XmlArray(ElementName = "My_Books")
     */
    /** @property 
     */
    public Book[] get_Books()
    {
        return books;
    } //get_Books

    /** @property 
     */
    public void set_Books(Book[] value)
    {
        books = value;
    } //set_Books
} //Library

public class Book
{
    public String title;
    public String author;
    public String isbn;
} //Book

public class Run
{
    public static void main(String[] args)
    {
        Run test = new Run();

        test.WriteBook("ArrayExample.xml");
    } //main

    public void WriteBook(String fileName)
    {
        XmlSerializer mySerializer = new XmlSerializer(Library.class.ToType());
        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.set_Books(myBooks);
        mySerializer.Serialize(t, myLibrary, ns);
        t.Close();
    } //WriteBook
} //Run

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft