XmlElementAttribute コンストラクタ (Type)
XmlElementAttribute クラスの新しいインスタンスを初期化し、XmlElementAttribute の適用先のメンバの型を指定します。この型が使用されるのは、その型を含むオブジェクトを XmlSerializer がシリアル化または逆シリアル化する場合です。
名前空間: System.Xml.Serialization
アセンブリ: System.Xml (system.xml.dll 内)
構文
'宣言
Public Sub New ( _
type As Type _
)
'使用
Dim type As Type
Dim instance As New XmlElementAttribute(type)
public XmlElementAttribute (
Type type
)
public:
XmlElementAttribute (
Type^ type
)
public XmlElementAttribute (
Type type
)
public function XmlElementAttribute (
type : Type
)
パラメータ
- type
メンバの型から派生したオブジェクトの Type。
解説
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
バージョン情報
.NET Framework
サポート対象 : 2.0、1.1、1.0
.NET Compact Framework
サポート対象 : 2.0、1.0
参照
関連項目
XmlElementAttribute クラス
XmlElementAttribute メンバ
System.Xml.Serialization 名前空間