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)

property XmlElementAttributes^ XmlElements {
	XmlElementAttributes^ 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.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 ref class Car
   String^ Name;

public ref class Plane
   String^ Name;

public ref class Truck
   String^ Name;

public ref class Train
   String^ Name;

public ref class Transportation

   // Subsequent code overrides these two XmlElementAttributes.

   ArrayList^ Vehicles;

// Return an XmlSerializer used for overriding.
XmlSerializer^ CreateOverrider()
   // Create the XmlAttributes and XmlAttributeOverrides objects.
   XmlAttributes^ attrs = gcnew XmlAttributes;
   XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;

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

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

   // Create the object and serialize it.
   Transportation^ myTransportation = gcnew Transportation;

   /* Create two new override objects that can be
      inserted into the array. */
   myTransportation->Vehicles = gcnew ArrayList;
   Truck^ myTruck = gcnew Truck;
   myTruck->Name = "MyTruck";
   Train^ myTrain = gcnew Train;
   myTrain->Name = "MyTrain";
   myTransportation->Vehicles->Add( myTruck );
   myTransportation->Vehicles->Add( myTrain );
   TextWriter^ writer = gcnew StreamWriter( filename );
   xSer->Serialize( writer, myTransportation );

int main()
   SerializeObject( "OverrideElement.xml" );

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top