XmlArrayAttribute.ElementName プロパティ
シリアル化された配列に与えられた、XML 要素の名前を取得または設定します。
Public Property ElementName As String
[C#]
public string ElementName {get; set;}
[C++]
public: __property String* get_ElementName();public: __property void set_ElementName(String*);
[JScript]
public function get ElementName() : String;public function set ElementName(String);
プロパティ値
シリアル化された配列の XML 要素名。既定値は、 XmlArrayAttribute が代入されたメンバの名前です。
解説
生成された XML 要素の名前をメンバの識別子と異なる名前にする場合は、 ElementName を指定します。
生成された XML ドキュメントが同一名のメンバと区別するために XML 名前空間を使用している場合は、同じ ElementName の値を複数のメンバに設定できます。XML ドキュメントでの名前空間の使用方法およびプリフィックス付きの名前の作成方法については、 XmlSerializerNamespaces のトピックを参照してください。
使用例
[Visual Basic, C#, C++] Book
項目の配列を返す Books
という名前のプロパティを含む Library
クラスのインスタンスをシリアル化する例を次に示します。この例では、 ElementName プロパティを使用して、XML 要素の配列の名前を Books
ではなく My_Books
とするように指定します。
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
[C#]
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();
}
}
[C++]
#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
{
public:
String* Title;
String* Author;
String* ISBN;
};
public __gc class Library
{
private:
Book* books[];
public:
[XmlArray(ElementName=S"My_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"http://wwww.contoso.com");
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;
mySerializer->Serialize(t,myLibrary,ns);
t->Close();
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
XmlArrayAttribute クラス | XmlArrayAttribute メンバ | System.Xml.Serialization 名前空間 | XmlSerializer | Namespace | Form