Export (0) Print
Expand All

XmlElementAttributes.Add Method

Adds an XmlElementAttribute to the collection.

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

Parameters

attribute
The XmlElementAttribute to add.

Return Value

The zero-based index of the newly added item.

Example

[Visual Basic, C#, C++] The following example creates two XmlElementAttribute objects and calls the Add method to add them to an XmlElementAttributes. The example then adds the XmlElementAttributes to an XmlAttributeOverrides, which is used to create an XmlSerializer that can serialize an instance of the Transportation class.

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

XmlElementAttributes Class | XmlElementAttributes Members | System.Xml.Serialization Namespace

Show:
© 2015 Microsoft