Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XML Deserialisation to an inherited class type based on an XML attribute

XML Deserialisation to an inherited class type based on an XML attribute

Scheduled Pinned Locked Moved C#
questionxmlhelptutorialannouncement
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Clive D Pottinger
    wrote on last edited by
    #1

    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 and TruckClass that inherit from AutoClass. 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 generates AutoClass objects and I need the results to be of type CarClass and TruckClass. I can't downcast Vehicles[0] to CarClass (or can I?). How do I get Vehicles to examine the type attribute of the auto element and create 2 CarClass objects and one TruckClass? I hope that is clear - if not, let me know.

    Clive Pottinger Victoria, BC

    M C 2 Replies Last reply
    0
    • C Clive D Pottinger

      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 and TruckClass that inherit from AutoClass. 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 generates AutoClass objects and I need the results to be of type CarClass and TruckClass. I can't downcast Vehicles[0] to CarClass (or can I?). How do I get Vehicles to examine the type attribute of the auto element and create 2 CarClass objects and one TruckClass? I hope that is clear - if not, let me know.

      Clive Pottinger Victoria, BC

      M Offline
      M Offline
      Muhammad Mazhar
      wrote on last edited by
      #2

      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...

      C 1 Reply Last reply
      0
      • M Muhammad Mazhar

        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...

        C Offline
        C Offline
        Clive D Pottinger
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • C Clive D Pottinger

          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 and TruckClass that inherit from AutoClass. 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 generates AutoClass objects and I need the results to be of type CarClass and TruckClass. I can't downcast Vehicles[0] to CarClass (or can I?). How do I get Vehicles to examine the type attribute of the auto element and create 2 CarClass objects and one TruckClass? I hope that is clear - if not, let me know.

          Clive Pottinger Victoria, BC

          C Offline
          C Offline
          Clive D Pottinger
          wrote on last edited by
          #4

          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 type CarClass and 1 object of type TruckClass - not 3 objects of type AutoClass. Edit: I know I could change the <auto> tags to <car> and <truck> and collect them in the Vehicles property - but that is not the goal. The XML tag should remain as <auto> but the objects created need to be of types CarClass and TruckClass. 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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups