XmlElementAttributes Class
Assembly: System.Xml (in system.xml.dll)
The XmlElementAttributes is returned by the XmlElements property of the XmlAttributes class. By using the XmlAttributeOverrides class and the XmlAttributes class, you can override the default way that the XmlSerializer serializes a class.
The following example serializes the Transportation class, which contains a single field named Vehicles that returns an ArrayList. The example first applies two instances of the XmlElementAttribute class to the Vehicles field that specifies the types of objects the XmlSerializer inserts into the array. The example then creates two XmlElementAttribute objects to override the behavior of the attributes applied to the Vehicles property. The two overriding objects are added to the XmlElementAttributes collection of an XmlAttributes. Lastly, the example adds the XmlAttributes to an XmlAttributeOverrides, allowing the XmlSerializer to insert the new object types into the ArrayList returned by the Vehicles field.
using System; using System.IO; using System.Xml.Serialization; using System.Collections; using System.Xml; public class Transportation { // Override 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"); } public XmlSerializer CreateOverrider() { // Create XmlAtrributes and XmlAttributeOverrides instances. XmlAttributes attrs = new XmlAttributes(); XmlAttributeOverrides xOver = new XmlAttributeOverrides(); /* Create an XmlElementAttributes object to override one of the attributes applied to 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. Transportation myTransportation = new Transportation(); /* Create two new, overriding objects that can be inserted into the Vehicles 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); } }
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Collections.*;
import System.Xml.*;
public class Transportation
{
// Override these two XmlElementAttributes.
/** @attribute XmlElement(Car.class)
@attribute XmlElement(Plane.class)
*/
public ArrayList vehicles;
} //Transportation
public class Car
{
public String name;
} //Car
public class Plane
{
public String name;
} //Plane
public class Truck
{
public String name;
} //Truck
public class Train
{
public String name;
} //Train
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
t.SerializeObject("OverrideElement.xml");
} //main
public XmlSerializer CreateOverrider()
{
// Create XmlAtrributes and XmlAttributeOverrides instances.
XmlAttributes attrs = new XmlAttributes();
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
/* Create an XmlElementAttributes object to override
one of the attributes applied to the Vehicles property. */
XmlElementAttribute xElement1 =
new XmlElementAttribute(Truck.class.ToType());
// Add the XmlElementAttribute to the collection.
attrs.get_XmlElements().Add(xElement1);
/* Create a second XmlElementAttribute and
add it to the collection. */
XmlElementAttribute xElement2 =
new XmlElementAttribute(Train.class.ToType());
attrs.get_XmlElements().Add(xElement2);
/* Add the XmlAttributes to the XmlAttributeOverrides,
specifying the member to override. */
xOver.Add(Transportation.class.ToType(), "vehicles", attrs);
// Create the XmlSerializer, and return it.
XmlSerializer xSer =
new XmlSerializer(Transportation.class.ToType(), xOver);
return xSer;
} //CreateOverrider
public void SerializeObject(String fileName)
{
// Create an XmlSerializer instance.
XmlSerializer xSer = CreateOverrider();
// Create the object.
Transportation myTransportation = new Transportation();
/* Create two new, overriding objects that can be
inserted into the Vehicles 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);
} //SerializeObject
} //Test
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.