次の方法で共有


XmlElementAttributes.Add メソッド

XmlElementAttribute をコレクションに追加します。

Public Function Add( _
   ByVal attribute As XmlElementAttribute _) As Integer
[C#]
public int Add(XmlElementAttributeattribute);
[C++]
public: int Add(XmlElementAttribute* attribute);
[JScript]
public function Add(
   attribute : XmlElementAttribute) : int;

パラメータ

戻り値

新しく追加された項目の 0 から始まるインデックス。

使用例

[Visual Basic, C#, C++] 2 つの XmlElementAttribute オブジェクトを作成し、 Add メソッドを呼び出して、これらのオブジェクトを XmlElementAttributes に追加する例を次に示します。この例では、次に XmlElementAttributesXmlAttributeOverrides に追加し、これを使用して、 Transportation クラスのインスタンスをシリアル化できる XmlSerializer を作成しています。

 
Public Function CreateOverrider() As XmlSerializer
    ' Create XmlAttributes and XmlAttributeOverrides instances.
    Dim attrs As New XmlAttributes()
    Dim xOver As New XmlAttributeOverrides()
    
    ' Create an XmlElementAttributes to override
    ' 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 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
End Class
 

[C#] 
public XmlSerializer CreateOverrider()
{
   // Create XmlAttributes and XmlAttributeOverrides instances.
   
   XmlAttributes attrs = new XmlAttributes();
   XmlAttributeOverrides xOver = 
   new XmlAttributeOverrides();
      
   /* Create an XmlElementAttributes to override 
      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 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;
}


[C++] 
public:
   XmlSerializer* CreateOverrider()
   {
      // Create XmlAttributes and XmlAttributeOverrides instances.

      XmlAttributes* attrs = new XmlAttributes();
      XmlAttributeOverrides* xOver = 
         new XmlAttributeOverrides();

      /* Create an XmlElementAttributes to override 
      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 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), S"Vehicles", attrs);

      // Create the XmlSerializer, and return it.
      XmlSerializer* xSer = new XmlSerializer
         (__typeof(Transportation), xOver);
      return xSer;
   }

[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 ファミリ

参照

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