XmlElementAttributes クラス
XmlSerializer がクラスをシリアル化する既定の方法をオーバーライドするために使用する、 XmlElementAttribute のコレクションを表します。
この型のすべてのメンバの一覧については、XmlElementAttributes メンバ を参照してください。
System.Object
System.Collections.CollectionBase
System.Xml.Serialization.XmlElementAttributes
Public Class XmlElementAttributes
Inherits CollectionBase
[C#]
public class XmlElementAttributes : CollectionBase
[C++]
public __gc class XmlElementAttributes : public CollectionBase
[JScript]
public class XmlElementAttributes extends CollectionBase
スレッドセーフ
この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。
解説
XmlElementAttributes は、 XmlAttributes クラスの XmlElements プロパティによって返されます。 XmlAttributeOverrides クラスと XmlAttributes クラスを使用して、 XmlSerializer がクラスをシリアル化する既定の方法をオーバーライドできます。
使用例
[Visual Basic, C#, C++] Transportation
クラスをシリアル化する例を次に示します。この例には、 ArrayList を返す Vehicles
という名前の単一のフィールドが含まれています。この例では、最初に XmlElementAttribute クラスの 2 つのインスタンスを Vehicles
フィールドに適用しており、各インスタンスは、 XmlSerializer が配列に挿入できるオブジェクトの型を指定しています。次に、2 つの XmlElementAttribute オブジェクトを作成し、Vehicles プロパティに適用される属性の動作をオーバーライドします。オーバーライドする側の 2 つのオブジェクトは、 XmlAttributes の XmlElementAttributes コレクションに追加されます。最後に、この XmlAttributes を XmlAttributeOverrides に追加します。これにより、 XmlSerializer が、 Vehicles
フィールドによって返される ArrayList に新しいオブジェクト型を挿入できるようになります。
Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports System.Collections
Imports System.Xml
Public Class Transportation
' Override these two XmlElementAttributes.
<XmlElement(GetType(Car)), _
XmlElement(GetType(Plane))> _
Public Vehicles As ArrayList
End Class
Public Class Car
Public Name As String
End Class
Public Class Plane
Public Name As String
End Class
Public Class Truck
Public Name As String
End Class
Public Class Train
Public Name As String
End Class
Public Class Test
Public Shared Sub Main()
Dim t As New Test()
t.SerializeObject("OverrideElement.xml")
End Sub
Public Function CreateOverrider() As XmlSerializer
' Create XmlAtrributes and XmlAttributeOverrides instances.
Dim attrs As New XmlAttributes()
Dim xOver As New XmlAttributeOverrides()
' Create an XmlElementAttributes object to override
' one of the attributes applied to the Vehicles property.
Dim xElement1 As New XmlElementAttribute(GetType(Truck))
' Add the XmlElementAttribute to the collection.
attrs.XmlElements.Add(xElement1)
' Create a second XmlElementAttribute and
' add it to the collection.
Dim xElement2 As New XmlElementAttribute(GetType(Train))
attrs.XmlElements.Add(xElement2)
' Add the XmlAttributes to the XmlAttributeOverrides,
' specifying the member to override.
xOver.Add(GetType(Transportation), "Vehicles", attrs)
' Create the XmlSerializer, and return it.
Dim xSer As New XmlSerializer(GetType(Transportation), xOver)
Return xSer
End Function
Public Sub SerializeObject(ByVal filename As String)
' Create an XmlSerializer instance.
Dim xSer As XmlSerializer = CreateOverrider()
' Create the object.
Dim myTransportation As New Transportation()
' Create two new, overriding objects that can be
' inserted into the Vehicles array.
myTransportation.Vehicles = New ArrayList()
Dim myTruck As New Truck()
myTruck.Name = "MyTruck"
Dim myTrain As New Train()
myTrain.Name = "MyTrain"
myTransportation.Vehicles.Add(myTruck)
myTransportation.Vehicles.Add(myTrain)
Dim writer As New StreamWriter(filename)
xSer.Serialize(writer, myTransportation)
End Sub
End Class
[C#]
using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections;
using System.Xml;
public class Transportation
{
// Override these two XmlElementAttributes.
[XmlElement(typeof(Car)),
XmlElement(typeof(Plane))]
public ArrayList Vehicles;
}
public class Car
{
public string Name;
}
public class Plane
{
public string Name;
}
public class Truck
{
public string Name;
}
public class Train
{
public string Name;
}
public class Test
{
public static void Main()
{
Test t = new Test();
t.SerializeObject("OverrideElement.xml");
}
public XmlSerializer CreateOverrider()
{
// Create XmlAtrributes and XmlAttributeOverrides instances.
XmlAttributes attrs = new XmlAttributes();
XmlAttributeOverrides xOver =
new XmlAttributeOverrides();
/* Create an XmlElementAttributes object to override
one of the attributes applied to the Vehicles property. */
XmlElementAttribute xElement1 =
new XmlElementAttribute(typeof(Truck));
// Add the XmlElementAttribute to the collection.
attrs.XmlElements.Add(xElement1);
/* Create a second XmlElementAttribute and
add it to the collection. */
XmlElementAttribute xElement2 =
new XmlElementAttribute(typeof(Train));
attrs.XmlElements.Add(xElement2);
/* Add the XmlAttributes to the XmlAttributeOverrides,
specifying the member to override. */
xOver.Add(typeof(Transportation), "Vehicles", attrs);
// Create the XmlSerializer, and return it.
XmlSerializer xSer = new XmlSerializer
(typeof(Transportation), xOver);
return xSer;
}
public void SerializeObject(string filename)
{
// Create an XmlSerializer instance.
XmlSerializer xSer = CreateOverrider();
// Create the object.
Transportation myTransportation =
new Transportation();
/* Create two new, overriding objects that can be
inserted into the Vehicles array. */
myTransportation.Vehicles = new ArrayList();
Truck myTruck = new Truck();
myTruck.Name = "MyTruck";
Train myTrain = new Train();
myTrain.Name = "MyTrain";
myTransportation.Vehicles.Add(myTruck);
myTransportation.Vehicles.Add(myTrain);
TextWriter writer = new StreamWriter(filename);
xSer.Serialize(writer, myTransportation);
}
}
[C++]
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Collections;
using namespace System::Xml;
public __gc class Car
{
public:
String* Name;
};
public __gc class Plane
{
public:
String* Name;
};
public __gc class Truck
{
public:
String* Name;
};
public __gc class Train
{
public:
String* Name;
};
public __gc class Transportation
{
// Override these two XmlElementAttributes.
public:
[XmlElement(__typeof(Car)),
XmlElement(__typeof(Plane))]
ArrayList* Vehicles;
};
XmlSerializer* CreateOverrider()
{
// Create XmlAtrributes and XmlAttributeOverrides instances.
XmlAttributes* attrs = new XmlAttributes();
XmlAttributeOverrides* xOver =
new XmlAttributeOverrides();
/* Create an XmlElementAttributes object to override
one of the attributes applied to the Vehicles property. */
XmlElementAttribute* xElement1 =
new XmlElementAttribute(__typeof(Truck));
// Add the XmlElementAttribute to the collection.
attrs->XmlElements->Add(xElement1);
/* Create a second XmlElementAttribute and
add it to the collection. */
XmlElementAttribute* xElement2 =
new XmlElementAttribute(__typeof(Train));
attrs->XmlElements->Add(xElement2);
/* Add the XmlAttributes to the XmlAttributeOverrides,
specifying the member to override. */
xOver->Add(__typeof(Transportation), S"Vehicles", attrs);
// Create the XmlSerializer, and return it.
XmlSerializer* xSer = new XmlSerializer
(__typeof(Transportation), xOver);
return xSer;
}
void SerializeObject(String* filename)
{
// Create an XmlSerializer instance.
XmlSerializer* xSer = CreateOverrider();
// Create the object.
Transportation* myTransportation =
new Transportation();
/* Create two new, overriding objects that can be
inserted into the Vehicles array. */
myTransportation->Vehicles = new ArrayList();
Truck* myTruck = new Truck();
myTruck->Name = S"MyTruck";
Train* myTrain = new Train();
myTrain->Name = S"MyTrain";
myTransportation->Vehicles->Add(myTruck);
myTransportation->Vehicles->Add(myTrain);
TextWriter* writer = new StreamWriter(filename);
xSer->Serialize(writer, myTransportation);
}
int main()
{
SerializeObject(S"OverrideElement.xml");
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System.Xml.Serialization
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
アセンブリ: System.Xml (System.Xml.dll 内)