共用方式為


XmlArrayItemAttribute.ElementName 屬性

定義

取得或設定產生的 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

屬性值

String

產生的 XML 項目的名稱。 預設值為成員識別項。

範例

下列範例會 ElementName 設定 和 Car 類別的 Vehicle 屬性,藉此變更為這些類別產生的 XML 專案 XmlSerializer 名稱。

// 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

適用於