Here's the promissed example:
using System;
using System.Xml;
using System.Xml.Serialization;
using System.Collections;
// NO XmlInclude attributes
// to declare derived types
public class Vehicle
{
public Vehicle(){}
public string Make;
public string Model;
public int Year;
}
public class Car : Vehicle
{
public Car() {}
public string VIN;
}
public class Motorcycle : Vehicle
{
public Motorcycle() {}
public bool VeryLoud;
}
public class ParkingLot
{
public ArrayList Cars;
}
public class AttributeOverrides
{
public static void Main()
{
ParkingLot lot = SetupParkingLot();
XmlTextWriter writer = new XmlTextWriter( Console.Out );
writer.Formatting = Formatting.Indented;
Console.WriteLine( "Writing Parking Lot Without Runtime Attributes:" );
SerializeParkingLot( writer, lot );
Console.WriteLine( "" );
writer.Close();
// need another writer because
// we can't write two more than one root element with the
// same XmlTextWriter instance
XmlTextWriter anotherWriter = new XmlTextWriter( Console.Out );
anotherWriter.Formatting = Formatting.Indented;
Console.WriteLine( "\n\nWriting Parking Lot With Runtime Attributes:" );
SerializeCustomParkingLot( anotherWriter, lot );
anotherWriter.Close();
}
private static ParkingLot SetupParkingLot()
{
ParkingLot lot = new ParkingLot();
lot.Cars = new ArrayList();
Car wifesCar = new Car();
wifesCar.Make = "Ford";
wifesCar.Model = "Explorer";
wifesCar.Year = 1997;
wifesCar.VIN = "ABC123DEF";
lot.Cars.Add( wifesCar );
return lot;
}
private static void SerializeParkingLot(XmlWriter writer,
ParkingLot parkingLot)
{
XmlSerializer xs = new XmlSerializer( typeof(ParkingLot),
new Type[] { typeof(Car) } );
xs.Serialize( writer, parkingLot );
}
private static void SerializeCustomParkingLot(XmlWriter writer,
ParkingLot parkingLot)
{
XmlAttributes carsAttributes = new XmlAttributes();
XmlAttributes classAttributes =
new XmlAttributes();
classAttributes.XmlRoot =
new XmlRootAttribute("ParkingLotRoot");
carsAttributes.XmlArrayItems.Add( new
XmlArrayItemAttribute("ParkedCar", typeof(Car)));
XmlAttributeOverrides overrides =
new XmlAttributeOverrides();
overrides.Add(typeof(ParkingLot), classAttributes);
overrides.Add(typeof(ParkingLot), "Cars", carsAttributes);
try
{
XmlSerializer xs = new XmlSerializer(
typeof(ParkingLot), overrides );
xs.Serialize( writer, parkingLot );
}
catch( InvalidOperationException )
{
System.Console.WriteLine( "Bad override attributes" );
}
}
}
and here's a link to the configuration management application block:
http://www.gotdotnet.com/Community/W...e-fa4bf2e3080f
--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor
"Werner B. Strydom" <bl*******@hotmail.com> wrote in message
news:uV**************@TK2MSFTNGP11.phx.gbl...
Hi
I wrote a class, Document, which has an Array of ITask objects. In order
to serialize the tasks contained within the tasks array, I marked each task
class with an attribute [Serializable], and added an entry to the task
collection property (of Document).
[
XmlElement("foo", typeof(FooTask)),
XmlElement("baa", typeof(BaaTask)),
XmlElement("taa", typeof(TaaTask)),
]
public TaskCollection Tasks
{
get { return m_tasks; }
}
The number of tasks has increased so I'm looking at possibilities to
improve maintenace. Is there a way I can dynamically create the mapping of element
name and its type for the array list? I would like to have a configuration
file with such information and load it at run time, rather than hardcoding
the element names.
Thanks
Werner