XmlArrayItemAttribute.ElementName 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置生成的 XML 元素的名称。
public:
property System::String ^ ElementName { System::String ^ get(); void set(System::String ^ value); };
public string ElementName { get; set; }
member this.ElementName : string with get, set
Public Property ElementName As String
属性值
生成的 XML 元素的名称。 默认值为成员标识符。
示例
以下示例设置ElementName和类的属性Vehicle
,从而更改为这些类生成的 XML 元素XmlSerializerCar
的名称。
// By default, this class results in XML elements named "Vehicle".
public ref class Vehicle
{
public:
String^ id;
};
// By default, this class results in XML elements named "Car".
public ref class Car: public Vehicle
{
public:
String^ Maker;
};
public ref class Transportation
{
public:
/* Specifies acceptable types and the ElementName generated
for each object type. */
[XmlArray("Vehicles")]
[XmlArrayItem(Vehicle::typeid,ElementName="Transport"),
XmlArrayItem(Car::typeid,ElementName="Automobile")]
array<Vehicle^>^MyVehicles;
};
public class Transportation
{
[XmlArray("Vehicles")]
/* Specifies acceptable types and the ElementName generated
for each object type. */
[XmlArrayItem(typeof(Vehicle), ElementName = "Transport"),
XmlArrayItem(typeof(Car), ElementName = "Automobile")]
public Vehicle[] MyVehicles;
}
// By default, this class results in XML elements named "Vehicle".
public class Vehicle
{
public string id;
}
// By default, this class results in XML elements named "Car".
public class Car:Vehicle
{
public string Maker;
}
Public Class Transportation
' Specifies acceptable types and the ElementName generated
' for each object type.
<XmlArray("Vehicles"), _
XmlArrayItem(GetType(Vehicle), ElementName := "Transport"), _
XmlArrayItem(GetType(Car), ElementName := "Automobile")> _
Public MyVehicles() As Vehicle
End Class
' By default, this class results in XML elements named "Vehicle".
Public Class Vehicle
Public id As String
End Class
' By default, this class results in XMl elements named "Car".
Public Class Car
Inherits Vehicle
Public Maker As String
End Class
注解
ElementName指定是否希望生成的 XML 元素的名称不同于成员的标识符。
如果生成的 XML 文档使用 XML 命名空间来区分相同命名的成员,则可以将相同的 ElementName 值设置为多个类成员。 有关如何在 XML 文档中使用命名空间和前缀名称的详细信息,请参阅 XmlSerializerNamespaces 类。