XmlElementAttributes 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表 XmlElementAttribute 物件的集合,XmlSerializer 使用這些物件來覆寫其序列化類別的預設方式。
public ref class XmlElementAttributes : System::Collections::IList
public ref class XmlElementAttributes : System::Collections::CollectionBase
public class XmlElementAttributes : System.Collections.IList
public class XmlElementAttributes : System.Collections.CollectionBase
type XmlElementAttributes = class
interface ICollection
interface IEnumerable
interface IList
type XmlElementAttributes = class
inherit CollectionBase
Public Class XmlElementAttributes
Implements IList
Public Class XmlElementAttributes
Inherits CollectionBase
- 繼承
-
XmlElementAttributes
- 繼承
- 實作
範例
下列範例會序列化 Transportation
類別,其中包含名為 的單一欄位,其會 ArrayList 傳 Vehicles
回 。 此範例會先將 類別的 XmlElementAttribute 兩個實例套用至 Vehicles
欄位,以指定插入陣列的物件類型 XmlSerializer 。 然後,此範例會建立兩 XmlElementAttribute 個 物件,以覆寫套用至 Vehicles
屬性的屬性行為。 這兩個 XmlAttributes 覆寫物件會新增至 XmlElementAttributes 的集合。 最後,此範例會將 加入 XmlAttributes 至 XmlAttributeOverrides ,讓 將 XmlSerializer 新的物件類型 ArrayList 插入欄位所傳回的 Vehicles
中。
#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 ref class Car
{
public:
String^ Name;
};
public ref class Plane
{
public:
String^ Name;
};
public ref class Truck
{
public:
String^ Name;
};
public ref class Train
{
public:
String^ Name;
};
public ref class Transportation
{
public:
// Override these two XmlElementAttributes.
[XmlElement(Car::typeid),
XmlElement(Plane::typeid)]
ArrayList^ Vehicles;
};
XmlSerializer^ CreateOverrider()
{
// Create XmlAtrributes and XmlAttributeOverrides instances.
XmlAttributes^ attrs = gcnew XmlAttributes;
XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
/* Create an XmlElementAttributes object to override
one of the attributes applied to the Vehicles property. */
XmlElementAttribute^ xElement1 = gcnew XmlElementAttribute( Truck::typeid );
// Add the XmlElementAttribute to the collection.
attrs->XmlElements->Add( xElement1 );
/* Create a second XmlElementAttribute and
add it to the collection. */
XmlElementAttribute^ xElement2 = gcnew XmlElementAttribute( Train::typeid );
attrs->XmlElements->Add( xElement2 );
/* Add the XmlAttributes to the XmlAttributeOverrides,
specifying the member to override. */
xOver->Add( Transportation::typeid, "Vehicles", attrs );
// Create the XmlSerializer, and return it.
XmlSerializer^ xSer = gcnew XmlSerializer( Transportation::typeid,xOver );
return xSer;
}
void SerializeObject( String^ filename )
{
// Create an XmlSerializer instance.
XmlSerializer^ xSer = CreateOverrider();
// Create the object.
Transportation^ myTransportation = gcnew Transportation;
/* Create two new, overriding objects that can be
inserted into the Vehicles array. */
myTransportation->Vehicles = gcnew ArrayList;
Truck^ myTruck = gcnew Truck;
myTruck->Name = "MyTruck";
Train^ myTrain = gcnew Train;
myTrain->Name = "MyTrain";
myTransportation->Vehicles->Add( myTruck );
myTransportation->Vehicles->Add( myTrain );
TextWriter^ writer = gcnew StreamWriter( filename );
xSer->Serialize( writer, myTransportation );
}
int main()
{
SerializeObject( "OverrideElement.xml" );
}
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);
}
}
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
備註
由 XmlElementAttributes 類別的 XmlAttributes 屬性傳 XmlElements 回 。 藉由使用 XmlAttributeOverrides 類別和 XmlAttributes 類別,您可以覆寫序列化類別的預設方式 XmlSerializer 。
建構函式
XmlElementAttributes() |
初始化 XmlElementAttributes 類別的新執行個體。 |
屬性
Capacity |
取得或設定 CollectionBase 可包含的項目數目。 (繼承來源 CollectionBase) |
Count |
取得 ICollection 中所包含的項目數。 |
Count |
取得 CollectionBase 執行個體中包含的元素數目。 這個屬性無法覆寫。 (繼承來源 CollectionBase) |
InnerList |
取得包含 ArrayList 執行個體中之元素清單的 CollectionBase。 (繼承來源 CollectionBase) |
Item[Int32] |
在指定的索引位置上取得或設定項目。 |
List |
取得包含 IList 執行個體中之元素清單的 CollectionBase。 (繼承來源 CollectionBase) |
方法
明確介面實作
擴充方法
Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
OfType<TResult>(IEnumerable) |
根據指定的型別來篩選 IEnumerable 的項目。 |
AsParallel(IEnumerable) |
啟用查詢的平行化作業。 |
AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |