XmlElementAttribute クラス
アセンブリ: System.Xml (system.xml.dll 内)

<AttributeUsageAttribute(AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter Or AttributeTargets.ReturnValue, AllowMultiple:=True)> _ Public Class XmlElementAttribute Inherits Attribute
[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter|AttributeTargets.ReturnValue, AllowMultiple=true)] public class XmlElementAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Property|AttributeTargets::Field|AttributeTargets::Parameter|AttributeTargets::ReturnValue, AllowMultiple=true)] public ref class XmlElementAttribute : public Attribute

XmlElementAttribute は、XmlSerializer がオブジェクトをシリアル化または逆シリアル化する方法を制御する一連の属性の 1 つです。類似する属性の完全な一覧については、「XML シリアル化を制御する属性」を参照してください。
通常、XML ドキュメントには XML 要素が含まれており、各 XML 要素は、開始タグ、終了タグ、タグ間のデータという 3 つの部分で構成されています。XML タグは入れ子にできます。つまり、タグ間のデータが XML 要素になっている場合もあります。これにより、ある要素で別の要素を囲むことができるため、ドキュメントにデータを階層的に格納できます。XML 要素には、属性を含むこともできます。
要素名や名前空間など、XML 要素の特性を制御するには、XmlElementAttribute をパブリック フィールドまたは読み取り/書き込み可能なパブリック プロパティに適用します。
XmlElementAttribute は、オブジェクトの配列を返すフィールドには何回でも適用できます。その理由は、その配列に挿入できる各種の型を、Type プロパティを使用して指定できるようにするためです。たとえば、次の C# コードの配列には、文字列と整数の両方を格納できます。
public class Things{ [XmlElement(DataType = typeof(string)), XmlElement(DataType = typeof(int))] public object[] StringsAndInts; }
ElementName プロパティの値を指定せずに XmlElementAttribute を何回も適用した場合、各要素には、配列に挿入できるオブジェクトの型の名前が付けられます。
配列を返すフィールドまたはプロパティに XmlElementAttribute を適用すると、その配列内の項目は XML 要素のシーケンスとしてエンコードされます。
これに対し、このようなフィールドまたはプロパティに XmlElementAttribute を適用しないと、それらが返す配列内の項目は単なる要素のシーケンスとしてエンコードされ、それぞれフィールドまたはプロパティの名前が付いた要素として入れ子になります。配列のシリアル化方法を制御する場合は、XmlArrayAttribute 属性と XmlArrayItemAttribute 属性を使用します。
Type プロパティを設定して、元のフィールドまたはプロパティ (XmlElementAttribute を適用したフィールドまたはプロパティ) の型から派生する型を指定できます。
フィールドまたはプロパティが ArrayList を返す場合は、そのメンバに XmlElementAttribute の複数のインスタンスを適用できます。各インスタンスの Type プロパティに、配列に挿入できるオブジェクトの型を設定します。
属性の使用方法については、「属性を使用したメタデータの拡張」を参照してください。
![]() |
---|

Group という名前のクラスをシリアル化し、そのメンバのいくつかに XmlElementAttribute を適用する例を次に示します。Employees という名前のフィールドは、Employee オブジェクトの配列を返します。この例では、XmlElementAttribute は、結果として生成される XML を入れ子にしないように指定しています。これは、配列内の項目の既定の動作です。
Imports System Imports System.Collections Imports System.IO Imports System.Xml.Serialization Public Class Group ' Set the element name and namespace of the XML element. <XmlElement(ElementName := "Members", _ Namespace := "http://www.cpandl.com")> _ Public Employees() As Employee <XmlElement(DataType := "double", _ ElementName := "Building")> _ Public GroupID As Double <XmlElement(DataType := "hexBinary")> _ Public HexBytes() As Byte <XmlElement(DataType := "boolean")> _ Public IsActive As Boolean <XmlElement(GetType(Manager))> _ Public Manager As Employee <XmlElement(GetType(Integer), _ ElementName := "ObjectNumber"), _ XmlElement(GetType(String), _ ElementName := "ObjectString")> _ Public ExtraInfo As ArrayList End Class Public Class Employee Public Name As String End Class Public Class Manager Inherits Employee Public Level As Integer End Class Public Class Run Public Shared Sub Main() Dim test As New Run() test.SerializeObject("FirstDoc.xml") test.DeserializeObject("FirstDoc.xml") End Sub Public Sub SerializeObject(filename As String) ' Create the XmlSerializer. Dim s As New XmlSerializer(GetType(Group)) ' To write the file, a TextWriter is required. Dim writer As New StreamWriter(filename) ' Create an instance of the group to serialize, and set ' its properties. Dim group As New Group() group.GroupID = 10.089f group.IsActive = False group.HexBytes = New Byte() {Convert.ToByte(100)} Dim x As New Employee() Dim y As New Employee() x.Name = "Jack" y.Name = "Jill" group.Employees = New Employee() {x, y} Dim mgr As New Manager() mgr.Name = "Sara" mgr.Level = 4 group.Manager = mgr ' Add a number and a string to the ' ArrayList returned by the ExtraInfo property. group.ExtraInfo = New ArrayList() group.ExtraInfo.Add(42) group.ExtraInfo.Add("Answer") ' Serialize the object, and close the TextWriter. s.Serialize(writer, group) writer.Close() End Sub Public Sub DeserializeObject(filename As String) Dim fs As New FileStream(filename, FileMode.Open) Dim x As New XmlSerializer(GetType(Group)) Dim g As Group = CType(x.Deserialize(fs), Group) Console.WriteLine(g.Manager.Name) Console.WriteLine(g.GroupID) Console.WriteLine(g.HexBytes(0)) Dim e As Employee For Each e In g.Employees Console.WriteLine(e.Name) Next e End Sub End Class
using System; using System.Collections; using System.IO; using System.Xml.Serialization; public class Group { /* Set the element name and namespace of the XML element. By applying an XmlElementAttribute to an array, you instruct the XmlSerializer to serialize the array as a series of XML elements, instead of a nested set of elements. */ [XmlElement( ElementName = "Members", Namespace = "http://www.cpandl.com")] public Employee[] Employees; [XmlElement(DataType = "double", ElementName = "Building")] public double GroupID; [XmlElement(DataType = "hexBinary")] public byte [] HexBytes; [XmlElement(DataType = "boolean")] public bool IsActive; [XmlElement(Type = typeof(Manager))] public Employee Manager; [XmlElement(typeof(int), ElementName = "ObjectNumber"), XmlElement(typeof(string), ElementName = "ObjectString")] public ArrayList ExtraInfo; } public class Employee { public string Name; } public class Manager:Employee{ public int Level; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("FirstDoc.xml"); test.DeserializeObject("FirstDoc.xml"); } public void SerializeObject(string filename) { // Create the XmlSerializer. XmlSerializer s = new XmlSerializer(typeof(Group)); // To write the file, a TextWriter is required. TextWriter writer = new StreamWriter(filename); /* Create an instance of the group to serialize, and set its properties. */ Group group = new Group(); group.GroupID = 10.089f; group.IsActive = false; group.HexBytes = new byte[1]{Convert.ToByte(100)}; Employee x = new Employee(); Employee y = new Employee(); x.Name = "Jack"; y.Name = "Jill"; group.Employees = new Employee[2]{x,y}; Manager mgr = new Manager(); mgr.Name = "Sara"; mgr.Level = 4; group.Manager = mgr; /* Add a number and a string to the ArrayList returned by the ExtraInfo property. */ group.ExtraInfo = new ArrayList(); group.ExtraInfo.Add(42); group.ExtraInfo.Add("Answer"); // Serialize the object, and close the TextWriter. s.Serialize(writer, group); writer.Close(); } public void DeserializeObject(string filename) { FileStream fs = new FileStream(filename, FileMode.Open); XmlSerializer x = new XmlSerializer(typeof(Group)); Group g = (Group) x.Deserialize(fs); Console.WriteLine(g.Manager.Name); Console.WriteLine(g.GroupID); Console.WriteLine(g.HexBytes[0]); foreach(Employee e in g.Employees) { Console.WriteLine(e.Name); } } }
#using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::Collections; using namespace System::IO; using namespace System::Xml::Serialization; public ref class Employee { public: String^ Name; }; public ref class Manager: public Employee { public: int Level; }; public ref class Group { public: /* Set the element name and namespace of the XML element. By applying an XmlElementAttribute to an array, you instruct the XmlSerializer to serialize the array as a series of XML elements, instead of a nested set of elements. */ [XmlElement( ElementName="Members", Namespace="http://www.cpandl.com")] array<Employee^>^Employees; [XmlElement(DataType="snippet1>", ElementName="Building")] double GroupID; [XmlElement(DataType="hexBinary")] array<Byte>^HexBytes; [XmlElement(DataType="boolean")] bool IsActive; [XmlElement(Type=::Manager::typeid)] Employee^ Manager; [XmlElement(Int32::typeid, ElementName="ObjectNumber"), XmlElement(String::typeid, ElementName="ObjectString")] ArrayList^ ExtraInfo; }; void SerializeObject( String^ filename ) { // Create the XmlSerializer. XmlSerializer^ s = gcnew XmlSerializer( Group::typeid ); // To write the file, a TextWriter is required. TextWriter^ writer = gcnew StreamWriter( filename ); /* Create an instance of the group to serialize, and set its properties. */ Group^ group = gcnew Group; group->GroupID = 10.089f; group->IsActive = false; array<Byte>^temp0 = {Convert::ToByte( 100 )}; group->HexBytes = temp0; Employee^ x = gcnew Employee; Employee^ y = gcnew Employee; x->Name = "Jack"; y->Name = "Jill"; array<Employee^>^temp1 = {x,y}; group->Employees = temp1; Manager^ mgr = gcnew Manager; mgr->Name = "Sara"; mgr->Level = 4; group->Manager = mgr; /* Add a number and a string to the ArrayList returned by the ExtraInfo property. */ group->ExtraInfo = gcnew ArrayList; group->ExtraInfo->Add( 42 ); group->ExtraInfo->Add( "Answer" ); // Serialize the object, and close the TextWriter. s->Serialize( writer, group ); writer->Close(); } void DeserializeObject( String^ filename ) { FileStream^ fs = gcnew FileStream( filename,FileMode::Open ); XmlSerializer^ x = gcnew XmlSerializer( Group::typeid ); Group^ g = dynamic_cast<Group^>(x->Deserialize( fs )); Console::WriteLine( g->Manager->Name ); Console::WriteLine( g->GroupID ); Console::WriteLine( g->HexBytes[ 0 ] ); IEnumerator^ myEnum = g->Employees->GetEnumerator(); while ( myEnum->MoveNext() ) { Employee^ e = safe_cast<Employee^>(myEnum->Current); Console::WriteLine( e->Name ); } } int main() { SerializeObject( "FirstDoc.xml" ); DeserializeObject( "FirstDoc.xml" ); }
import System.*; import System.Collections.*; import System.IO.*; import System.Xml.Serialization.*; public class Group { /* Set the element name and namespace of the XML element. By applying an XmlElementAttribute to an array, you instruct the XmlSerializer to serialize the array as a series of XML elements, instead of a nested set of elements. */ /** @attribute XmlElement(ElementName = "Members", Namespace = "http://www.cpandl.com") */ public Employee employees[]; /** @attribute XmlElement(DataType = "double", ElementName = "Building") */ public double groupID; /** @attribute XmlElement(DataType = "hexBinary") */ public ubyte hexBytes[]; /** @attribute XmlElement(DataType = "boolean") */ public boolean isActive; /** @attribute XmlElement(Type = Manager.class) */ public Employee manager; /** @attribute XmlElement(int.class, ElementName = "ObjectNumber") @attribute XmlElement(String.class, ElementName = "ObjectString") */ public ArrayList extraInfo; } //Group public class Employee { public String name; } //Employee public class Manager extends Employee { public int level; } //Manager public class Run { public static void main(String[] args) { Run test = new Run(); test.SerializeObject("FirstDoc.xml"); test.DeserializeObject("FirstDoc.xml"); } //main public void SerializeObject(String fileName) { // Create the XmlSerializer. XmlSerializer s = new XmlSerializer(Group.class.ToType()); // To write the file, a TextWriter is required. TextWriter writer = new StreamWriter(fileName); /* Create an instance of the group to serialize, and set its properties. */ Group group = new Group(); group.groupID = 10.089f; group.isActive = false; group.hexBytes = new ubyte[] { Convert.ToByte(100) }; Employee x = new Employee(); Employee y = new Employee(); x.name = "Jack"; y.name = "Jill"; group.employees = new Employee[] { x, y }; Manager mgr = new Manager(); mgr.name = "Sara"; mgr.level = 4; group.manager = mgr; /* Add a number and a string to the ArrayList returned by the ExtraInfo property. */ group.extraInfo = new ArrayList(); group.extraInfo.Add((Int32)42); group.extraInfo.Add("Answer"); // Serialize the object, and close the TextWriter. s.Serialize(writer, group); writer.Close(); } //SerializeObject public void DeserializeObject(String fileName) { FileStream fs = new FileStream(fileName, FileMode.Open); XmlSerializer x = new XmlSerializer(Group.class.ToType()); Group g = (Group)x.Deserialize(fs); Console.WriteLine(g.manager.name); Console.WriteLine(g.groupID); Console.WriteLine(g.hexBytes.get_Item(0)); for (int iCtr = 0; iCtr < g.employees.length; iCtr++) { Employee e = g.employees[iCtr]; Console.WriteLine(e.name); } } //DeserializeObject } //Run

System.Attribute
System.Xml.Serialization.XmlElementAttribute


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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlElementAttribute メンバ
System.Xml.Serialization 名前空間
XmlArrayAttribute クラス
XmlAttributeOverrides クラス
XmlAttributes クラス
XmlElementAttributes
XmlAttributes.XmlElements プロパティ
XmlRootAttribute
XmlSerializer
XmlAttributes クラス
その他の技術情報
XML シリアル化の概要
方法 : XML ストリームの代替要素名を指定する
属性を使用した XML シリアル化の制御
XML シリアル化の例
XML スキーマ定義ツール (Xsd.exe)
XmlElementAttribute コンストラクタ ()
アセンブリ: System.Xml (system.xml.dll 内)



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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlElementAttribute コンストラクタ (String, Type)
アセンブリ: System.Xml (system.xml.dll 内)

Dim elementName As String Dim type As Type Dim instance As New XmlElementAttribute(elementName, type)

既定では、XmlSerializer でクラスのインスタンスをシリアル化するときは、メンバ名が XML 要素名として使用されます。たとえば、Vehicle という名前のフィールドは、Vehicle という名前の XML 要素を生成します。ただし、Cars などの別の名前の要素が必要な場合には、その名前を elementName パラメータに渡します。
type パラメータを使用して、基本クラスから派生する型を指定します。たとえば、MyAnimal という名前のプロパティが Animal オブジェクトを返す場合を想定します。このオブジェクトを拡張するには、Mammal という名前の新しいクラスを Animal クラスから継承して作成します。XmlSerializer に対して、MyAnimal プロパティをシリアル化するときに Mammal クラスを受け入れるように指示するには、Mammal クラスの Type をコンストラクタに渡します。

Instrument オブジェクトの配列を返すフィールド Instruments を 1 つ含んでいるクラス Orchestra をシリアル化する例を次に示します。Brass という名前の 2 番目のクラスが Instrument クラスから継承されます。この例では、XmlElementAttribute が Instruments フィールドに適用され、Brass 型が指定されています。これにより、Instruments フィールドが Brass オブジェクトを受け入れるようになります。また、ElementName プロパティを設定することによって、XML 要素の名前も指定します。
Option Strict Option Explicit Imports System Imports System.IO Imports System.Xml.Serialization Imports Microsoft.VisualBasic Public Class Orchestra Public Instruments() As Instrument End Class Public Class Instrument Public Name As String End Class Public Class Brass Inherits Instrument Public IsValved As Boolean End Class Public Class Run Public Shared Sub Main() Dim test As New Run() test.SerializeObject("Override.xml") test.DeserializeObject("Override.xml") End Sub 'Main Public Sub SerializeObject(filename As String) ' To write the file, a TextWriter is required. Dim writer As New StreamWriter(filename) Dim attrOverrides As New XmlAttributeOverrides() Dim attrs As New XmlAttributes() ' Creates an XmlElementAttribute that overrides the Instrument type. Dim attr As New XmlElementAttribute(GetType(Brass)) attr.ElementName = "Brass" ' Adds the element to the collection of elements. attrs.XmlElements.Add(attr) attrOverrides.Add(GetType(Orchestra), "Instruments", attrs) ' Creates the XmlSerializer using the XmlAttributeOverrides. Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides) ' Creates the object to serialize. Dim band As New Orchestra() ' Creates an object of the derived type. Dim i As New Brass() i.Name = "Trumpet" i.IsValved = True Dim myInstruments() As Instrument = {i} band.Instruments = myInstruments s.Serialize(writer, band) writer.Close() End Sub Public Sub DeserializeObject(filename As String) Dim attrOverrides As New XmlAttributeOverrides() Dim attrs As New XmlAttributes() ' Create an XmlElementAttribute that override the Instrument type. Dim attr As New XmlElementAttribute(GetType(Brass)) attr.ElementName = "Brass" ' Add the element to the collection of elements. attrs.XmlElements.Add(attr) attrOverrides.Add(GetType(Orchestra), "Instruments", attrs) ' Create the XmlSerializer using the XmlAttributeOverrides. Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides) Dim fs As New FileStream(filename, FileMode.Open) Dim band As Orchestra = CType(s.Deserialize(fs), Orchestra) Console.WriteLine("Brass:") ' Deserializing differs from serializing. To read the ' derived-object values, declare an object of the derived ' type (Brass) and cast the Instrument instance to it. Dim b As Brass Dim i As Instrument For Each i In band.Instruments b = CType(i, Brass) Console.WriteLine((b.Name + ControlChars.Cr + b.IsValved.ToString())) Next i End Sub End Class
using System; using System.IO; using System.Xml.Serialization; public class Orchestra { public Instrument[] Instruments; } public class Instrument { public string Name; } public class Brass:Instrument{ public bool IsValved; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("Override.xml"); test.DeserializeObject("Override.xml"); } public void SerializeObject(string filename) { // To write the file, a TextWriter is required. TextWriter writer = new StreamWriter(filename); XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute that overrides the Instrument type. XmlElementAttribute attr = new XmlElementAttribute(typeof(Brass)); attr.ElementName = "Brass"; // Adds the element to the collection of elements. attrs.XmlElements.Add(attr); attrOverrides.Add(typeof(Orchestra), "Instruments", attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(typeof(Orchestra), attrOverrides); // Creates the object to serialize. Orchestra band = new Orchestra(); // Creates an object of the derived type. Brass i = new Brass(); i.Name = "Trumpet"; i.IsValved = true; Instrument[] myInstruments = {i}; band.Instruments = myInstruments; s.Serialize(writer,band); writer.Close(); } public void DeserializeObject(string filename) { XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute that override the Instrument type. XmlElementAttribute attr = new XmlElementAttribute(typeof(Brass)); attr.ElementName = "Brass"; // Adds the element to the collection of elements. attrs.XmlElements.Add(attr); attrOverrides.Add(typeof(Orchestra), "Instruments", attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(typeof(Orchestra), attrOverrides); FileStream fs = new FileStream(filename, FileMode.Open); Orchestra band = (Orchestra) s.Deserialize(fs); Console.WriteLine("Brass:"); /* Deserializing differs from serializing. To read the derived-object values, declare an object of the derived type (Brass) and cast the Instrument instance to it. */ Brass b; foreach(Instrument i in band.Instruments) { b= (Brass)i; Console.WriteLine( b.Name + "\n" + b.IsValved); } } }
#using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::IO; using namespace System::Xml::Serialization; public ref class Instrument { public: String^ Name; }; public ref class Brass: public Instrument { public: bool IsValved; }; public ref class Orchestra { public: array<Instrument^>^Instruments; }; void SerializeObject( String^ filename ) { // To write the file, a TextWriter is required. TextWriter^ writer = gcnew StreamWriter( filename ); XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides; XmlAttributes^ attrs = gcnew XmlAttributes; // Creates an XmlElementAttribute that overrides the Instrument type. XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid ); attr->ElementName = "Brass"; // Adds the element to the collection of elements. attrs->XmlElements->Add( attr ); attrOverrides->Add( Orchestra::typeid, "Instruments", attrs ); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides ); // Creates the object to serialize. Orchestra^ band = gcnew Orchestra; // Creates an object of the derived type. Brass^ i = gcnew Brass; i->Name = "Trumpet"; i->IsValved = true; array<Instrument^>^myInstruments = {i}; band->Instruments = myInstruments; s->Serialize( writer, band ); writer->Close(); } void DeserializeObject( String^ filename ) { XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides; XmlAttributes^ attrs = gcnew XmlAttributes; // Creates an XmlElementAttribute that override the Instrument type. XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid ); attr->ElementName = "Brass"; // Adds the element to the collection of elements. attrs->XmlElements->Add( attr ); attrOverrides->Add( Orchestra::typeid, "Instruments", attrs ); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides ); FileStream^ fs = gcnew FileStream( filename,FileMode::Open ); Orchestra^ band = dynamic_cast<Orchestra^>(s->Deserialize( fs )); Console::WriteLine( "Brass:" ); /* Deserializing differs from serializing. To read the derived-object values, declare an object of the derived type (Brass) and cast the Instrument instance to it. */ Brass^ b; System::Collections::IEnumerator^ myEnum = band->Instruments->GetEnumerator(); while ( myEnum->MoveNext() ) { Instrument^ i = safe_cast<Instrument^>(myEnum->Current); b = dynamic_cast<Brass^>(i); Console::WriteLine( "{0}\n{1}", b->Name, b->IsValved ); } } int main() { SerializeObject( "Override.xml" ); DeserializeObject( "Override.xml" ); }
import System.*; import System.IO.*; import System.Xml.Serialization.*; public class Orchestra { public Instrument instruments[]; } //Orchestra public class Instrument { public String name; } //Instrument public class Brass extends Instrument { public boolean isValved; } //Brass public class Run { public static void main(String[] args) { Run test = new Run(); test.SerializeObject("Override.xml"); test.DeserializeObject("Override.xml"); } //main public void SerializeObject(String fileName) { // To write the file, a TextWriter is required. TextWriter writer = new StreamWriter(fileName); XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute that overrides the Instrument type. XmlElementAttribute attr = new XmlElementAttribute(Brass.class.ToType()); attr.set_ElementName("Brass"); // Adds the element to the collection of elements. attrs.get_XmlElements().Add(attr); attrOverrides.Add(Orchestra.class.ToType(), "instruments", attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(Orchestra.class.ToType(), attrOverrides); // Creates the object to serialize. Orchestra band = new Orchestra(); // Creates an object of the derived type. Brass i = new Brass(); i.name = "Trumpet"; i.isValved = true; Instrument myInstruments[] = { i }; band.instruments = myInstruments; s.Serialize(writer, band); writer.Close(); } //SerializeObject public void DeserializeObject(String fileName) { XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute that override the Instrument type. XmlElementAttribute attr = new XmlElementAttribute(Brass.class.ToType()); attr.set_ElementName("Brass"); // Adds the element to the collection of elements. attrs.get_XmlElements().Add(attr); attrOverrides.Add(Orchestra.class.ToType(), "instruments", attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(Orchestra.class.ToType(), attrOverrides); FileStream fs = new FileStream(fileName, FileMode.Open); Orchestra band = (Orchestra)s.Deserialize(fs); Console.WriteLine("Brass:"); /* Deserializing differs from serializing. To read the derived-object values, declare an object of the derived type (Brass) and cast the Instrument instance to it. */ Brass b; for (int iCtr = 0; iCtr < band.instruments.length; iCtr++) { Instrument i = band.instruments[iCtr]; b = (Brass)i; Console.WriteLine(b.name + "\n" + Convert.ToString(b.isValved)); } } //DeserializeObject } //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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlElementAttribute コンストラクタ (Type)
アセンブリ: System.Xml (system.xml.dll 内)


type パラメータを使用して、基本クラスから派生する型を指定します。たとえば、MyAnimal という名前のプロパティが Animal オブジェクトを返す場合を想定します。このオブジェクトを拡張するには、Mammal という名前の新しいクラスを Animal クラスから継承して作成します。XmlSerializer に対して、MyAnimal プロパティをシリアル化するときに Mammal クラスを受け入れるように指示するには、Mammal クラスの Type をコンストラクタに渡します。

Instrument オブジェクトの配列を返すフィールド Instruments を 1 つ含んでいるクラス Orchestra をシリアル化する例を次に示します。Brass という名前の 2 番目のクラスが Instrument クラスから継承されます。この例では、XmlElementAttribute が Instruments フィールドに適用され、Brass 型が指定されています。これにより、Instruments フィールドが Brass オブジェクトを受け入れるようになります。また、ElementName プロパティを設定することによって、XML 要素の名前も指定します。
Option Strict Option Explicit Imports System Imports System.IO Imports System.Xml.Serialization Imports Microsoft.VisualBasic Public Class Orchestra Public Instruments() As Instrument End Class Public Class Instrument Public Name As String End Class Public Class Brass Inherits Instrument Public IsValved As Boolean End Class Public Class Run Public Shared Sub Main() Dim test As New Run() test.SerializeObject("Override.xml") test.DeserializeObject("Override.xml") End Sub 'Main Public Sub SerializeObject(filename As String) ' To write the file, a TextWriter is required. Dim writer As New StreamWriter(filename) Dim attrOverrides As New XmlAttributeOverrides() Dim attrs As New XmlAttributes() ' Creates an XmlElementAttribute that overrides the Instrument type. Dim attr As New XmlElementAttribute(GetType(Brass)) attr.ElementName = "Brass" ' Adds the element to the collection of elements. attrs.XmlElements.Add(attr) attrOverrides.Add(GetType(Orchestra), "Instruments", attrs) ' Creates the XmlSerializer using the XmlAttributeOverrides. Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides) ' Creates the object to serialize. Dim band As New Orchestra() ' Creates an object of the derived type. Dim i As New Brass() i.Name = "Trumpet" i.IsValved = True Dim myInstruments() As Instrument = {i} band.Instruments = myInstruments s.Serialize(writer, band) writer.Close() End Sub Public Sub DeserializeObject(filename As String) Dim attrOverrides As New XmlAttributeOverrides() Dim attrs As New XmlAttributes() ' Create an XmlElementAttribute that override the Instrument type. Dim attr As New XmlElementAttribute(GetType(Brass)) attr.ElementName = "Brass" ' Add the element to the collection of elements. attrs.XmlElements.Add(attr) attrOverrides.Add(GetType(Orchestra), "Instruments", attrs) ' Create the XmlSerializer using the XmlAttributeOverrides. Dim s As New XmlSerializer(GetType(Orchestra), attrOverrides) Dim fs As New FileStream(filename, FileMode.Open) Dim band As Orchestra = CType(s.Deserialize(fs), Orchestra) Console.WriteLine("Brass:") ' Deserializing differs from serializing. To read the ' derived-object values, declare an object of the derived ' type (Brass) and cast the Instrument instance to it. Dim b As Brass Dim i As Instrument For Each i In band.Instruments b = CType(i, Brass) Console.WriteLine((b.Name + ControlChars.Cr + b.IsValved.ToString())) Next i End Sub End Class
using System; using System.IO; using System.Xml.Serialization; public class Orchestra { public Instrument[] Instruments; } public class Instrument { public string Name; } public class Brass:Instrument{ public bool IsValved; } public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("Override.xml"); test.DeserializeObject("Override.xml"); } public void SerializeObject(string filename) { // To write the file, a TextWriter is required. TextWriter writer = new StreamWriter(filename); XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute that overrides the Instrument type. XmlElementAttribute attr = new XmlElementAttribute(typeof(Brass)); attr.ElementName = "Brass"; // Adds the element to the collection of elements. attrs.XmlElements.Add(attr); attrOverrides.Add(typeof(Orchestra), "Instruments", attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(typeof(Orchestra), attrOverrides); // Creates the object to serialize. Orchestra band = new Orchestra(); // Creates an object of the derived type. Brass i = new Brass(); i.Name = "Trumpet"; i.IsValved = true; Instrument[] myInstruments = {i}; band.Instruments = myInstruments; s.Serialize(writer,band); writer.Close(); } public void DeserializeObject(string filename) { XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute that override the Instrument type. XmlElementAttribute attr = new XmlElementAttribute(typeof(Brass)); attr.ElementName = "Brass"; // Adds the element to the collection of elements. attrs.XmlElements.Add(attr); attrOverrides.Add(typeof(Orchestra), "Instruments", attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(typeof(Orchestra), attrOverrides); FileStream fs = new FileStream(filename, FileMode.Open); Orchestra band = (Orchestra) s.Deserialize(fs); Console.WriteLine("Brass:"); /* Deserializing differs from serializing. To read the derived-object values, declare an object of the derived type (Brass) and cast the Instrument instance to it. */ Brass b; foreach(Instrument i in band.Instruments) { b= (Brass)i; Console.WriteLine( b.Name + "\n" + b.IsValved); } } }
#using <System.Xml.dll> #using <System.dll> using namespace System; using namespace System::IO; using namespace System::Xml::Serialization; public ref class Instrument { public: String^ Name; }; public ref class Brass: public Instrument { public: bool IsValved; }; public ref class Orchestra { public: array<Instrument^>^Instruments; }; void SerializeObject( String^ filename ) { // To write the file, a TextWriter is required. TextWriter^ writer = gcnew StreamWriter( filename ); XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides; XmlAttributes^ attrs = gcnew XmlAttributes; // Creates an XmlElementAttribute that overrides the Instrument type. XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid ); attr->ElementName = "Brass"; // Adds the element to the collection of elements. attrs->XmlElements->Add( attr ); attrOverrides->Add( Orchestra::typeid, "Instruments", attrs ); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides ); // Creates the object to serialize. Orchestra^ band = gcnew Orchestra; // Creates an object of the derived type. Brass^ i = gcnew Brass; i->Name = "Trumpet"; i->IsValved = true; array<Instrument^>^myInstruments = {i}; band->Instruments = myInstruments; s->Serialize( writer, band ); writer->Close(); } void DeserializeObject( String^ filename ) { XmlAttributeOverrides^ attrOverrides = gcnew XmlAttributeOverrides; XmlAttributes^ attrs = gcnew XmlAttributes; // Creates an XmlElementAttribute that override the Instrument type. XmlElementAttribute^ attr = gcnew XmlElementAttribute( Brass::typeid ); attr->ElementName = "Brass"; // Adds the element to the collection of elements. attrs->XmlElements->Add( attr ); attrOverrides->Add( Orchestra::typeid, "Instruments", attrs ); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer^ s = gcnew XmlSerializer( Orchestra::typeid,attrOverrides ); FileStream^ fs = gcnew FileStream( filename,FileMode::Open ); Orchestra^ band = dynamic_cast<Orchestra^>(s->Deserialize( fs )); Console::WriteLine( "Brass:" ); /* Deserializing differs from serializing. To read the derived-object values, declare an object of the derived type (Brass) and cast the Instrument instance to it. */ Brass^ b; System::Collections::IEnumerator^ myEnum = band->Instruments->GetEnumerator(); while ( myEnum->MoveNext() ) { Instrument^ i = safe_cast<Instrument^>(myEnum->Current); b = dynamic_cast<Brass^>(i); Console::WriteLine( "{0}\n{1}", b->Name, b->IsValved ); } } int main() { SerializeObject( "Override.xml" ); DeserializeObject( "Override.xml" ); }
import System.*; import System.IO.*; import System.Xml.Serialization.*; public class Orchestra { public Instrument instruments[]; } //Orchestra public class Instrument { public String name; } //Instrument public class Brass extends Instrument { public boolean isValved; } //Brass public class Run { public static void main(String[] args) { Run test = new Run(); test.SerializeObject("Override.xml"); test.DeserializeObject("Override.xml"); } //main public void SerializeObject(String fileName) { // To write the file, a TextWriter is required. TextWriter writer = new StreamWriter(fileName); XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute that overrides the Instrument type. XmlElementAttribute attr = new XmlElementAttribute(Brass.class.ToType()); attr.set_ElementName("Brass"); // Adds the element to the collection of elements. attrs.get_XmlElements().Add(attr); attrOverrides.Add(Orchestra.class.ToType(), "instruments", attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(Orchestra.class.ToType(), attrOverrides); // Creates the object to serialize. Orchestra band = new Orchestra(); // Creates an object of the derived type. Brass i = new Brass(); i.name = "Trumpet"; i.isValved = true; Instrument myInstruments[] = { i }; band.instruments = myInstruments; s.Serialize(writer, band); writer.Close(); } //SerializeObject public void DeserializeObject(String fileName) { XmlAttributeOverrides attrOverrides = new XmlAttributeOverrides(); XmlAttributes attrs = new XmlAttributes(); // Creates an XmlElementAttribute that override the Instrument type. XmlElementAttribute attr = new XmlElementAttribute(Brass.class.ToType()); attr.set_ElementName("Brass"); // Adds the element to the collection of elements. attrs.get_XmlElements().Add(attr); attrOverrides.Add(Orchestra.class.ToType(), "instruments", attrs); // Creates the XmlSerializer using the XmlAttributeOverrides. XmlSerializer s = new XmlSerializer(Orchestra.class.ToType(), attrOverrides); FileStream fs = new FileStream(fileName, FileMode.Open); Orchestra band = (Orchestra)s.Deserialize(fs); Console.WriteLine("Brass:"); /* Deserializing differs from serializing. To read the derived-object values, declare an object of the derived type (Brass) and cast the Instrument instance to it. */ Brass b; for (int iCtr = 0; iCtr < band.instruments.length; iCtr++) { Instrument i = band.instruments[iCtr]; b = (Brass)i; Console.WriteLine(b.name + "\n" + Convert.ToString(b.isValved)); } } //DeserializeObject } //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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlElementAttribute コンストラクタ (String)
アセンブリ: System.Xml (system.xml.dll 内)


既定では、XmlSerializer でクラスのインスタンスをシリアル化するときは、メンバ名が XML 要素名として使用されます。たとえば、Vehicle という名前のフィールドは、Vehicle という名前の XML 要素を生成します。ただし、Cars などの別の名前の要素が必要な場合には、その名前を elementName パラメータに渡します。

Vehicles という名前のフィールドが 1 つある単純なクラスの例を次に示します。この例では、XmlElementAttribute をフィールドに適用し、elementName パラメータを指定しています。これにより、XmlSerializer に対して、"Vehicles" ではなく "Cars" という名前で XML 要素を生成するように指示できます。

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
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


XmlElementAttribute コンストラクタ

名前 | 説明 |
---|---|
XmlElementAttribute () | XmlElementAttribute クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
XmlElementAttribute (String) | XML 要素の名前を指定して、XmlElementAttribute クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
XmlElementAttribute (Type) | XmlElementAttribute クラスの新しいインスタンスを初期化し、XmlElementAttribute の適用先のメンバの型を指定します。この型が使用されるのは、その型を含むオブジェクトを XmlSerializer がシリアル化または逆シリアル化する場合です。 .NET Compact Framework によってサポートされています。 |
XmlElementAttribute (String, Type) | XmlElementAttribute の新しいインスタンスを初期化し、XmlElementAttribute の適用先であるメンバの XML 要素の名前と派生型を指定します。このメンバ型が使用されるのは、その型を含むオブジェクトを XmlSerializer がシリアル化する場合です。 .NET Compact Framework によってサポートされています。 |

XmlElementAttribute プロパティ


関連項目
XmlElementAttribute クラスSystem.Xml.Serialization 名前空間
XmlArrayAttribute クラス
XmlAttributeOverrides クラス
XmlAttributes クラス
XmlElementAttributes
XmlAttributes.XmlElements プロパティ
XmlRootAttribute
XmlSerializer
XmlAttributes クラス
その他の技術情報
XML シリアル化の概要方法 : XML ストリームの代替要素名を指定する
属性を使用した XML シリアル化の制御
XML シリアル化の例
XML スキーマ定義ツール (Xsd.exe)
XmlElementAttribute メソッド

名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 ( Attribute から継承されます。) |
![]() | GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 ( Attribute から継承されます。) |
![]() | GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 ( Attribute から継承されます。) |
![]() | GetHashCode | このインスタンスのハッシュ コードを返します。 ( Attribute から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 ( Attribute から継承されます。) |
![]() | IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 ( Attribute から継承されます。) |
![]() | Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 ( Attribute から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

関連項目
XmlElementAttribute クラスSystem.Xml.Serialization 名前空間
XmlArrayAttribute クラス
XmlAttributeOverrides クラス
XmlAttributes クラス
XmlElementAttributes
XmlAttributes.XmlElements プロパティ
XmlRootAttribute
XmlSerializer
XmlAttributes クラス
その他の技術情報
XML シリアル化の概要方法 : XML ストリームの代替要素名を指定する
属性を使用した XML シリアル化の制御
XML シリアル化の例
XML スキーマ定義ツール (Xsd.exe)
XmlElementAttribute メンバ
パブリック フィールドやパブリック プロパティを保持するオブジェクトを XmlSerializer がシリアル化または逆シリアル化するときに、それらのフィールドやプロパティが XML 要素を表すかどうかを示します。
XmlElementAttribute データ型で公開されるメンバを以下の表に示します。



名前 | 説明 | |
---|---|---|
![]() | Equals | オーバーロードされます。 ( Attribute から継承されます。) |
![]() | GetCustomAttribute | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 (Attribute から継承されます。) |
![]() | GetCustomAttributes | オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 (Attribute から継承されます。) |
![]() | GetHashCode | このインスタンスのハッシュ コードを返します。 (Attribute から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | IsDefaultAttribute | 派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 (Attribute から継承されます。) |
![]() | IsDefined | オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 (Attribute から継承されます。) |
![]() | Match | 派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

関連項目
XmlElementAttribute クラスSystem.Xml.Serialization 名前空間
XmlArrayAttribute クラス
XmlAttributeOverrides クラス
XmlAttributes クラス
XmlElementAttributes
XmlAttributes.XmlElements プロパティ
XmlRootAttribute
XmlSerializer
XmlAttributes クラス
その他の技術情報
XML シリアル化の概要方法 : XML ストリームの代替要素名を指定する
属性を使用した XML シリアル化の制御
XML シリアル化の例
XML スキーマ定義ツール (Xsd.exe)
- XmlElementAttributeのページへのリンク