XmlElementAttributes.Add(XmlElementAttribute) Metod

Definition

Lägger till en XmlElementAttribute i samlingen.

public:
 int Add(System::Xml::Serialization::XmlElementAttribute ^ attribute);
public int Add(System.Xml.Serialization.XmlElementAttribute attribute);
member this.Add : System.Xml.Serialization.XmlElementAttribute -> int
Public Function Add (attribute As XmlElementAttribute) As Integer

Parametrar

attribute
XmlElementAttribute

Att XmlElementAttribute lägga till.

Returer

Det nollbaserade indexet för det nyligen tillagda objektet.

Exempel

I följande exempel skapas två XmlElementAttribute objekt och metoden anropas Add för att lägga till dem i en XmlElementAttributes. Exemplet lägger XmlElementAttributes sedan till i en XmlAttributeOverrides, som används för att skapa en XmlSerializer som kan serialisera en instans av Transportation klassen.

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

Gäller för