Freigeben über


XmlArrayAttribute.ElementName-Eigenschaft

Ruft den für das serialisierte Array angegebenen XML-Elementnamen ab oder legt diesen fest.

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

Syntax

'Declaration
Public Property ElementName As String
'Usage
Dim instance As XmlArrayAttribute
Dim value As String

value = instance.ElementName

instance.ElementName = value
public string ElementName { get; set; }
public:
property String^ ElementName {
    String^ get ();
    void set (String^ value);
}
/** @property */
public String get_ElementName ()

/** @property */
public void set_ElementName (String value)
public function get ElementName () : String

public function set ElementName (value : String)

Eigenschaftenwert

Der XML-Elementname des serialisierten Arrays. Der Standardname ist der Name des Members, dem XmlArrayAttribute zugewiesen ist.

Hinweise

Geben Sie einen ElementName an, wenn sich der Name des generierten XML-Elements vom Bezeichner des Members unterscheiden soll.

Sie können denselben ElementName-Wert auf mehrere Member festlegen, wenn das generierte XML-Dokument Member mit identischen Namen mithilfe von XML-Namespaces unterscheidet. Weitere Informationen darüber, wie im XML-Dokument Namespaces verwendet und Namen mit Präfixen erstellt werden, finden Sie unter XmlSerializerNamespaces.

Beispiel

Im folgenden Beispiel wird eine Instanz der Library-Klasse serialisiert, die die Eigenschaft Books enthält, die ein Array von Book-Elementen zurückgibt. Im Beispiel wird mit der ElementName-Eigenschaft angegeben, dass das Array von XML-Elementen nicht Books, sondern My_Books genannt werden soll.

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()
        Get
            Return myBooks
        End Get
        Set
            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()
        test.WriteBook("ArrayExample.xml")
    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)
        t.Close()
    End Sub
End Class
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();
   }
}
   
#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public ref class Book
{
public:
   String^ Title;
   String^ Author;
   String^ ISBN;
};

public ref class Library
{
private:
   array<Book^>^books;

public:

   [XmlArray(ElementName="My_Books")]
   property array<Book^>^ Books 
   {
      array<Book^>^ get()
      {
         return books;
      }

      void set( array<Book^>^value )
      {
         books = value;
      }
   }
};

int main()
{
   String^ filename = "ArrayExample.xml";
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Library::typeid );
   TextWriter^ t = gcnew StreamWriter( filename );
   XmlSerializerNamespaces^ ns = gcnew XmlSerializerNamespaces;
   ns->Add( "bk", "http://wwww.contoso.com" );
   Book^ b1 = gcnew Book;
   b1->Title = "MyBook Title";
   b1->Author = "An Author";
   b1->ISBN = "00000000";
   Book^ b2 = gcnew Book;
   b2->Title = "Another Title";
   b2->Author = "Another Author";
   b2->ISBN = "0000000";
   Library^ myLibrary = gcnew Library;
   array<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

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

XmlArrayAttribute-Klasse
XmlArrayAttribute-Member
System.Xml.Serialization-Namespace
XmlSerializer
Namespace
Form