Hi,
is it possible to serialize a top-level class with a collection of items?
For example, is this possible:
<ParkingLot ...>
<ParkedCar><Make>Ford</Make></ParkedCar>
<ParkedMotorcycle><Make>Honda</Make></ParkedMotorcycle>
<ParkedCar><Make>Hyondai</Make></ParkedCar>
<ParkedCar><Make>Volvo</Make></ParkedCar>
.... lots of other cars and motorcycles ...
</ParkingLot>
I know it is possible to serialize a known number of vehicles, with a class
like this:
public class ParkingLot
{
public ParkingLot()
{
Vehicles = new Vehicle[2];
}
[XmlElement(ElementName="ParkedCar", Type=typeof(Car))]
[XmlElement(ElementName="ParkedMotorcycle", Type=typeof(Motorcycle))]
public Vehicle[] Vehicles;
}
(both Car and Motorcycle derive from Vehicle).
But what if I don't know in advance how many Vehicles are going to be
serialized? I do not want to have an extra parent element around all my
vehicles as such:
<ParkingLot ...>
<ParkedVehicles>
<ParkedCar><Make>Ford</Make></ParkedCar>
<ParkedMotorcycle><Make>Honda</Make></ParkedMotorcycle>
</ParkedVehicles>
</ParkingLot>
I am grateful for any tips you can give me.
Thank you!
--
Daniel