Export (0) Print
Expand All

XmlAttributes.XmlElements Property

Gets a collection of objects that specify how the XmlSerializer serializes a public field or read/write property as an XML element.

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

public XmlElementAttributes XmlElements { get; }

Property Value

Type: System.Xml.Serialization.XmlElementAttributes
An XmlElementAttributes that contains a collection of XmlElementAttribute objects.

For each overridden member that is serialized as an XML element, you must add a new XmlElementAttribute to an XmlElementAttributes by calling the Add method. By default, an XmlElementAttributes object is created and assigned to the XmlElements property.

The following example serializes the Transportation class, which contains a single field named Vehicles that returns an ArrayList. The example applies two XmlElementAttribute attributes to the Vehicles field. The example creates two XmlElementAttribute objects and adds them to the XmlElementAttributes collection of an XmlAttributes object. To allow the array to accept different object types, the XmlAttributes object is added to the XmlAttributeOverrides object.

using System;
using System.IO;
using System.Xml.Serialization;
using System.Collections;
using System.Xml;

public class Transportation
{
   // Subsequent code overrides these two XmlElementAttributes.
   [XmlElement(typeof(Car)),
   XmlElement(typeof(Plane))]
   public ArrayList Vehicles;
}

public class Car
{
   public string Name;
}

public class Plane
{
   public string Name;
}
public class Truck
{
   public string Name;
}
public class Train
{
   public string Name;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
      t.SerializeObject("OverrideElement.xml");
   }

   // Return an XmlSerializer used for overriding. 
   public XmlSerializer CreateOverrider()
   {
      // Create the XmlAttributes and XmlAttributeOverrides objects.
      XmlAttributes attrs = new XmlAttributes();

      XmlAttributeOverrides xOver = 
      new XmlAttributeOverrides();


      /* Create an XmlElementAttribute 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 it 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 void SerializeObject(string filename)
   {
      // Create an XmlSerializer instance.
      XmlSerializer xSer = CreateOverrider();

      // Create the object and serialize it.
      Transportation myTransportation = 
      new Transportation();

      /* Create two new override objects that can be
      inserted into the array. */
      myTransportation.Vehicles = new ArrayList();
      Truck myTruck = new Truck();
      myTruck.Name = "MyTruck";

      Train myTrain = new Train();
      myTrain.Name = "MyTrain";

      myTransportation.Vehicles.Add(myTruck);
      myTransportation.Vehicles.Add(myTrain);

      TextWriter writer = new StreamWriter(filename);
      xSer.Serialize(writer, myTransportation);

   }
}
#using <mscorlib.dll>
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml::Serialization;
using namespace System::Collections;
using namespace System::Xml;

public __gc class Car
{
public:
   String* Name;
};

public __gc class Plane
{
public:
   String* Name;
};

public __gc class Truck
{
public:
   String* Name;
};

public __gc class Train
{
public:
   String* Name;
};

public __gc class Transportation
{
   // Subsequent code overrides these two XmlElementAttributes.
public:
   [XmlElement(__typeof(Car)),
      XmlElement(__typeof(Plane))]
   ArrayList* Vehicles;
};

// Return an XmlSerializer used for overriding.
XmlSerializer* CreateOverrider()
{
   // Create the XmlAttributes and XmlAttributeOverrides objects.
   XmlAttributes* attrs = new XmlAttributes();

   XmlAttributeOverrides* xOver = 
      new XmlAttributeOverrides();

   /* Create an XmlElementAttribute 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 it 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;
}

void SerializeObject(String* filename)
{
   // Create an XmlSerializer instance.
   XmlSerializer* xSer = CreateOverrider();

   // Create the object and serialize it.
   Transportation* myTransportation = 
      new Transportation();

   /* Create two new override objects that can be
   inserted into the array. */
   myTransportation->Vehicles = new ArrayList();
   Truck* myTruck = new Truck();
   myTruck->Name = S"MyTruck";

   Train* myTrain = new Train();
   myTrain->Name = S"MyTrain";

   myTransportation->Vehicles->Add(myTruck);
   myTransportation->Vehicles->Add(myTrain);

   TextWriter* writer = new StreamWriter(filename);
   xSer->Serialize(writer, myTransportation);

}

int main()
{
   SerializeObject(S"OverrideElement.xml");
}

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

Community Additions

ADD
Show:
© 2014 Microsoft