Freigeben über


XmlElementAttributes.Add-Methode

Fügt der Auflistung ein XmlElementAttribute hinzu.

Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)

Syntax

'Declaration
Public Function Add ( _
    attribute As XmlElementAttribute _
) As Integer
'Usage
Dim instance As XmlElementAttributes
Dim attribute As XmlElementAttribute
Dim returnValue As Integer

returnValue = instance.Add(attribute)
public int Add (
    XmlElementAttribute attribute
)
public:
int Add (
    XmlElementAttribute^ attribute
)
public int Add (
    XmlElementAttribute attribute
)
public function Add (
    attribute : XmlElementAttribute
) : int

Parameter

Rückgabewert

Der nullbasierte Index des neu hinzugefügten Elements.

Beispiel

Im folgenden Beispiel werden zwei XmlElementAttribute-Objekte erstellt und die Add-Methode aufgerufen, um diese einem XmlElementAttributes hinzuzufügen. Anschließend wird das XmlElementAttributes einem XmlAttributeOverrides hinzugefügt und mit diesem ein XmlSerializer erstellt, der eine Instanz der Transportation-Klasse serialisieren kann.

    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
 
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;
}
public:
   XmlSerializer^ CreateOverrider()
   {
      // Create XmlAttributes and XmlAttributeOverrides instances.

      XmlAttributes^ attrs = gcnew XmlAttributes;
      XmlAttributeOverrides^ xOver =
         gcnew XmlAttributeOverrides;

      /* Create an XmlElementAttributes to override 
            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 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;
   }
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(Truck.class.ToType());

    // Add the XmlElementAttribute to the collection.
    attrs.get_XmlElements().Add(xElement1);

    /* Create a second XmlElementAttribute, and 
       add to the collection. */
    XmlElementAttribute xElement2 =
        new XmlElementAttribute(Train.class.ToType());
    attrs.get_XmlElements().Add(xElement2);

    /* Add the XmlAttributes to the XmlAttributeOverrides,
       specifying the member to override. */
    xOver.Add(Transportation.class.ToType(), "Vehicles", attrs);

    // Create the XmlSerializer, and return it.
    XmlSerializer xSer =
        new XmlSerializer(Transportation.class.ToType(), xOver);
    return xSer;
} //CreateOverrider

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

XmlElementAttributes-Klasse
XmlElementAttributes-Member
System.Xml.Serialization-Namespace