XML Deserialisation to an inherited class type based on an XML attribute
-
Hi folks. This is probably a simple one for the XML gurus, but it has me stumped. I am trying deserialise an XML file, which is easy enough. But now I need to create objects based on an XML element's attribute, and I can't see how to do it. Here is a simplified version of the problem. I started with a file that looks like this:
<fleet>
<auto>boss's limo</auto>
<auto<delivery truck</auto>
<auto<employee's rusty corvair</auto>
</fleet>Easy enough to deserialise:
public class AutoClass { ... }
[XmlRoot("fleet")]
public class FleetClass
{
[XmlElement("auto")
public AutoClass[] Vehicles { ... }
}But now, I need to change the classes around. I need a
CarClass
andTruckClass
that inherit fromAutoClass
. So I created:public class CarClass : AutoClass { ... }
public class TruckClass : AutoClass { ... }Additionally, the file should look like this
<fleet>
<auto type="car">boss's limo</auto>
<auto type="truck"/>delivery truck</auto>
<auto type="car">employee's rusty corvair</auto>
</fleet>But the
Vehicles
property generatesAutoClass
objects and I need the results to be of typeCarClass
andTruckClass
. I can't downcastVehicles[0]
toCarClass
(or can I?). How do I getVehicles
to examine thetype
attribute of theauto
element and create 2CarClass
objects and oneTruckClass
? I hope that is clear - if not, let me know.Clive Pottinger Victoria, BC
-
Hi folks. This is probably a simple one for the XML gurus, but it has me stumped. I am trying deserialise an XML file, which is easy enough. But now I need to create objects based on an XML element's attribute, and I can't see how to do it. Here is a simplified version of the problem. I started with a file that looks like this:
<fleet>
<auto>boss's limo</auto>
<auto<delivery truck</auto>
<auto<employee's rusty corvair</auto>
</fleet>Easy enough to deserialise:
public class AutoClass { ... }
[XmlRoot("fleet")]
public class FleetClass
{
[XmlElement("auto")
public AutoClass[] Vehicles { ... }
}But now, I need to change the classes around. I need a
CarClass
andTruckClass
that inherit fromAutoClass
. So I created:public class CarClass : AutoClass { ... }
public class TruckClass : AutoClass { ... }Additionally, the file should look like this
<fleet>
<auto type="car">boss's limo</auto>
<auto type="truck"/>delivery truck</auto>
<auto type="car">employee's rusty corvair</auto>
</fleet>But the
Vehicles
property generatesAutoClass
objects and I need the results to be of typeCarClass
andTruckClass
. I can't downcastVehicles[0]
toCarClass
(or can I?). How do I getVehicles
to examine thetype
attribute of theauto
element and create 2CarClass
objects and oneTruckClass
? I hope that is clear - if not, let me know.Clive Pottinger Victoria, BC
I have done a similar job where I need to serialize/deserialize object to/from XML. Use visual studio XSD support to autogenerate classes that automatically support XML serialization. Raed following for this http://sharpertutorials.com/using-xsd-tool-to-generate-classes-from-xml/[^] Next job serialization/eserialisation can be easily carried out by following class http://www.koders.com/csharp/fid4FAEFA4E8A1DB8CBD93A4E1AF811BD685E5661BF.aspx?s="Nike"[^]
Share your experience with others Check my Blog...
-
I have done a similar job where I need to serialize/deserialize object to/from XML. Use visual studio XSD support to autogenerate classes that automatically support XML serialization. Raed following for this http://sharpertutorials.com/using-xsd-tool-to-generate-classes-from-xml/[^] Next job serialization/eserialisation can be easily carried out by following class http://www.koders.com/csharp/fid4FAEFA4E8A1DB8CBD93A4E1AF811BD685E5661BF.aspx?s="Nike"[^]
Share your experience with others Check my Blog...
Thanks for the speedy reply. However, I don't see how the XSD tool will help in this case. From the documentation here (http://msdn.microsoft.com/en-us/library/x6c1kb0s(VS.80).aspx) it seems that the tool will simply generate code for classes based on the XML elements found. I don't see how to use it to generate a class based on an XML element's attribute.
Clive Pottinger Victoria, BC
-
Hi folks. This is probably a simple one for the XML gurus, but it has me stumped. I am trying deserialise an XML file, which is easy enough. But now I need to create objects based on an XML element's attribute, and I can't see how to do it. Here is a simplified version of the problem. I started with a file that looks like this:
<fleet>
<auto>boss's limo</auto>
<auto<delivery truck</auto>
<auto<employee's rusty corvair</auto>
</fleet>Easy enough to deserialise:
public class AutoClass { ... }
[XmlRoot("fleet")]
public class FleetClass
{
[XmlElement("auto")
public AutoClass[] Vehicles { ... }
}But now, I need to change the classes around. I need a
CarClass
andTruckClass
that inherit fromAutoClass
. So I created:public class CarClass : AutoClass { ... }
public class TruckClass : AutoClass { ... }Additionally, the file should look like this
<fleet>
<auto type="car">boss's limo</auto>
<auto type="truck"/>delivery truck</auto>
<auto type="car">employee's rusty corvair</auto>
</fleet>But the
Vehicles
property generatesAutoClass
objects and I need the results to be of typeCarClass
andTruckClass
. I can't downcastVehicles[0]
toCarClass
(or can I?). How do I getVehicles
to examine thetype
attribute of theauto
element and create 2CarClass
objects and oneTruckClass
? I hope that is clear - if not, let me know.Clive Pottinger Victoria, BC
Perhaps I need to clarify further. I would like to deserialise this XML
<fleet>
<auto type="car">boss's limo</auto>
<auto type="truck"/>delivery truck</auto>
<auto type="car">employee's rusty corvair</auto>
</fleet>into these classes
public class AutoClass { ... }
[XmlRoot("fleet")]
public class FleetClass
{
[XmlElement("auto")
public AutoClass[] Vehicles { ... }
}
public class CarClass : AutoClass { ... }
public class TruckClass : AutoClass { ... }so that
XmlSerializer s = new XmlSerializer( typeof( FleetClass ) );
TextReader r = new StreamReader( "example.xml" );
FleetClass myFleet = (FleetClass)s.Deserialize( r );
AutoClass[] myVehicles = myFleet.Vehicles;should result in
myVehicles
containing 2 objects of typeCarClass
and 1 object of typeTruckClass
- not 3 objects of typeAutoClass
. Edit: I know I could change the<auto>
tags to<car>
and<truck>
and collect them in theVehicles
property - but that is not the goal. The XML tag should remain as<auto>
but the objects created need to be of typesCarClass
andTruckClass
. Is there any way to decorate the classes and properties to accomplish this?Clive Pottinger Victoria, BC
modified on Thursday, July 23, 2009 12:11 PM