次の方法で共有


XmlArrayItemAttribute.ElementName プロパティ

生成された XML 要素の名前を取得または設定します。

Public Property ElementName As String
[C#]
public string ElementName {get; set;}
[C++]
public: __property String* get_ElementName();public: __property void set_ElementName(String*);
[JScript]
public function get ElementName() : String;public function set ElementName(String);

プロパティ値

生成された XML 要素の名前。既定値は、メンバ識別子です。

解説

生成された XML 要素の名前をメンバの識別子と異なる名前にする場合は、 ElementName を指定します。

生成された XML ドキュメントが同一名のメンバを区別するために XML 名前空間を使用している場合は、同じ ElementName 値を複数のクラス メンバに設定できます。XML ドキュメントでの名前空間やプリフィックス付き名の使用方法については、 XmlSerializerNamespaces クラスのトピックを参照してください。

使用例

[Visual Basic, C#, C++] Vehicle クラスおよび Car クラスの ElementName プロパティを設定することにより、 XmlSerializer がこれらのクラスに対して生成する XML 要素の名前を変更する例を次に示します。

 
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


[C#] 
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;
}


[C++] 
// By default, this class results in XML elements named "Vehicle". 
public __gc class Vehicle
{
public:
   String* id;
};

// By default, this class results in XML elements named "Car". 
public __gc class Car:public Vehicle
{
public:
   String* Maker;
};

public __gc class Transportation
{  
public:
   [XmlArray(S"Vehicles")]
   /* Specifies acceptable types and the ElementName generated 
      for each object type. */
   [XmlArrayItem(__typeof(Vehicle), ElementName = S"Transport"), 
   XmlArrayItem(__typeof(Car), ElementName = S"Automobile")]
   Vehicle* MyVehicles[];
};

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

参照

XmlArrayItemAttribute クラス | XmlArrayItemAttribute メンバ | System.Xml.Serialization 名前空間