Here's the promissed example:
using System;
using System.Xml;
using System.Xml.Seri alization;
using System.Collecti ons;
// 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 AttributeOverri des
{
public static void Main()
{
ParkingLot lot = SetupParkingLot ();
XmlTextWriter writer = new XmlTextWriter( Console.Out );
writer.Formatti ng = Formatting.Inde nted;
Console.WriteLi ne( "Writing Parking Lot Without Runtime Attributes:" );
SerializeParkin gLot( writer, lot );
Console.WriteLi ne( "" );
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.F ormatting = Formatting.Inde nted;
Console.WriteLi ne( "\n\nWritin g Parking Lot With Runtime Attributes:" );
SerializeCustom ParkingLot( anotherWriter, lot );
anotherWriter.C lose();
}
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 SerializeParkin gLot(XmlWriter writer,
ParkingLot parkingLot)
{
XmlSerializer xs = new XmlSerializer( typeof(ParkingL ot),
new Type[] { typeof(Car) } );
xs.Serialize( writer, parkingLot );
}
private static void SerializeCustom ParkingLot(XmlW riter writer,
ParkingLot parkingLot)
{
XmlAttributes carsAttributes = new XmlAttributes() ;
XmlAttributes classAttributes =
new XmlAttributes() ;
classAttributes .XmlRoot =
new XmlRootAttribut e("ParkingLotRo ot");
carsAttributes. XmlArrayItems.A dd( new
XmlArrayItemAtt ribute("ParkedC ar", typeof(Car)));
XmlAttributeOve rrides overrides =
new XmlAttributeOve rrides();
overrides.Add(t ypeof(ParkingLo t), classAttributes );
overrides.Add(t ypeof(ParkingLo t), "Cars", carsAttributes) ;
try
{
XmlSerializer xs = new XmlSerializer(
typeof(ParkingL ot), overrides );
xs.Serialize( writer, parkingLot );
}
catch( InvalidOperatio nException )
{
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*******@hotm ail.com> wrote in message
news:uV******** ******@TK2MSFTN GP11.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