XmlArrayAttribute.ElementName Propriété

Définition

Obtient ou définit le nom d'élément XML donné au tableau sérialisé.Gets or sets the XML element name given to the serialized array.

public:
 property System::String ^ ElementName { System::String ^ get(); void set(System::String ^ value); };
public string ElementName { get; set; }
member this.ElementName : string with get, set
Public Property ElementName As String

Valeur de propriété

Nom d'élément XML du tableau sérialisé.The XML element name of the serialized array. Par défaut, il s'agit du nom du membre auquel XmlArrayAttribute est assigné.The default is the name of the member to which the XmlArrayAttribute is assigned.

Exemples

L’exemple suivant sérialise une instance de la classe Library qui contient une propriété nommée Books qui retourne un tableau d’éléments Book.The following example serializes an instance of the Library class that contains a property named Books that returns an array of Book items. L’exemple utilise la propriété ElementName pour spécifier que le tableau d’éléments XML doit être nommé My_Books plutôt que Books.The example uses the ElementName property to specify that the array of XML elements should be named My_Books rather than Books.

#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();
}
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();
   }
}
   
Option Explicit
Option Strict

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

Remarques

Spécifiez un ElementName lorsque vous souhaitez que le nom d’élément XML généré soit différent de l’identificateur du membre.Specify an ElementName when you want the generated XML element name to differ from the member's identifier.

Vous pouvez définir la même valeur ElementName sur plusieurs membres tant que le document XML généré utilise des espaces de noms XML pour faire la distinction entre les membres portant le même nom.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. Pour plus d’informations sur l’utilisation d’espaces de noms et sur la création de noms préfixés dans le document XML, consultez XmlSerializerNamespaces.For more details about using namespaces and creating prefixed names in the XML document, see XmlSerializerNamespaces.

S’applique à

Voir aussi