XmlElementAttributes 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示 XmlSerializer 用于重写其序列化类的默认方式的 XmlElementAttribute 对象的集合。
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
类,该类包含一个名为 Vehicles
的字段,该 ArrayList字段返回 。 该示例首先将 类的 XmlElementAttribute 两个实例应用于 Vehicles
字段,该字段指定插入数组中的对象 XmlSerializer 类型。 然后,该示例创建两个 XmlElementAttribute 对象来替代应用于 属性 Vehicles
的属性的行为。 这两个XmlAttributes重写对象将添加到 XmlElementAttributes 的集合中。 最后,该示例将 添加到 XmlAttributes ,XmlAttributeOverrides允许 XmlSerializer 将新的对象类型插入 字段返回的 Vehicles
中ArrayList。
#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由 XmlElements 类的 XmlAttributes 属性返回。 通过使用 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。 |