Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

XmlElementAttributes.Add Method

Adds an XmlElementAttribute to the collection.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml (in System.Xml.dll)
public int Add(
	XmlElementAttribute attribute
)

Return Value

Type: System.Int32
The zero-based index of the newly added item.

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.

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 = 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;
   }

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.