473,382 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Deserialize an abstract object

Look at the Code Snippet below:
It is not possible to Deserialize the Animal in animalStr directly
because the type of the animal is not known. Deserializing into the
abstract type will not work because the xml serilization in animalStr
contains more than an Animal, it contains either Cat or Dog. Is there a
neat way of dealing with deserilization of abstract types?

------------- Code Snippet -------------
abstract public class Animal
{
public abstract string GetAnimalType();
}

public class Dog : Animal
{
public string type = "Dog";
public override string GetAnimalType()
{
return type;
}
}

public class Cat : Animal
{
public string type = "Cat";
public string CatColor = "Grey";
public override string GetAnimalType()
{
return type;
}
}

class Test
{
[STAThread]
static void Main(string[] args)
{
string animalStr="";
//animalStr contains an XML serilization of either Dog or Cat

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Animal));
StringReader stringReader = new StringReader(animalStr);

Animal animal = (Animal)xmlSerializer.Deserialize(stringReader);
Console.WriteLine("Animal type:{0}", animal.GetAnimalType());
}
}

------------- Possible Solution -------------
Maybe with the use of a helper object eventhough its not very neat:

class AbstractXMLObject{
public string type;// either "Cat" or "Dog"
public string xml; // serilization of either Cat or Dog
}

class Test
{
[STAThread]
static void Main(string[] args)
{
AbstractXMLObject abstractXMLObject = new AbstractXMLObject;
//abstractXMLObject type and xml is set

XmlSerializer xmlSerializer;
if abstractXMLObject.type=="Dog" {
xmlSerializer = new XmlSerializer(typeof(Dog));
}
if abstractXMLObject.type=="Cat" {
xmlSerializer = new XmlSerializer(typeof(Cat));
}
StringReader stringReader = new StringReader(animalStr);
Animal animal=(Animal)xmlSerializer.Deserialize(abstractX MLObject.xml);
Console.WriteLine("Animal type:{0}", animal.GetAnimalType());
}
}

Nov 12 '05 #1
1 7823
Another option would be to put XmlInclude attributes on the Animal class.
[XmlInclude(typeof(Dog))]
[XmlInclude(typeof(Cat))]
public class Animal
{
....
}
But what if the the XML document contains an "Elephant" object, that also
inherits from Animal, but is defined in another assembly?
Because the assembly containing Elephant references the assembly containing
Animal, you can't add the XmlInclude attribute on Animal, because that would
also require a reference in the other direction, and this is not allowed...

I haven't been able to solve this one...
"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:%2********************@TK2MSFTNGP11.phx.gbl.. .
If the animal is contained within a collection or structure, you can do it
by attributing the property or field with XmlElement to discriminate
between
the various concrete types.

Eg,

public class Container {
[XmlElement("Dog", typeof(Dog))]
[XmlElement("Cat", typeof(Cat))]
public Animal[] menagerie;
}
de-serializing from this XML:
<Container>
<Cat>
<NumberOfMiceKilled>63</NumberOfMiceKilled>
<TypicalSleepingHoursPerDay>18</TypicalSleepingHoursPerDay>
</Cat>
<Dog>
<KnownTricks>
<Trick>Fetch</Trick>
<Trick>Rollover</Trick>
</KnownTricks>
<HasFleas>true</HasFleas>
<WillEatTableScraps>true</WillEatTableScraps>
</Dog>
</Container>

will give you a Container, with

menagerie[0].type= Cat
menagerie[1].type= Dog

-Dino

"Kristian Kjems" <kj***@msn.com> wrote in message
news:Xn*************************@207.46.248.16...
Look at the Code Snippet below:
It is not possible to Deserialize the Animal in animalStr directly
because the type of the animal is not known. Deserializing into the
abstract type will not work because the xml serilization in animalStr
contains more than an Animal, it contains either Cat or Dog. Is there a
neat way of dealing with deserilization of abstract types?

------------- Code Snippet -------------
abstract public class Animal
{
public abstract string GetAnimalType();
}

public class Dog : Animal
{
public string type = "Dog";
public override string GetAnimalType()
{
return type;
}
}

public class Cat : Animal
{
public string type = "Cat";
public string CatColor = "Grey";
public override string GetAnimalType()
{
return type;
}
}

class Test
{
[STAThread]
static void Main(string[] args)
{
string animalStr="";
//animalStr contains an XML serilization of either Dog or Cat

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Animal));
StringReader stringReader = new StringReader(animalStr);

Animal animal = (Animal)xmlSerializer.Deserialize(stringReader);
Console.WriteLine("Animal type:{0}", animal.GetAnimalType());
}
}

------------- Possible Solution -------------
Maybe with the use of a helper object eventhough its not very neat:

class AbstractXMLObject{
public string type;// either "Cat" or "Dog"
public string xml; // serilization of either Cat or Dog
}

class Test
{
[STAThread]
static void Main(string[] args)
{
AbstractXMLObject abstractXMLObject = new AbstractXMLObject;
//abstractXMLObject type and xml is set

XmlSerializer xmlSerializer;
if abstractXMLObject.type=="Dog" {
xmlSerializer = new XmlSerializer(typeof(Dog));
}
if abstractXMLObject.type=="Cat" {
xmlSerializer = new XmlSerializer(typeof(Cat));
}
StringReader stringReader = new StringReader(animalStr);
Animal animal=(Animal)xmlSerializer.Deserialize(abstractX MLObject.xml);
Console.WriteLine("Animal type:{0}", animal.GetAnimalType());
}
}


Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sean McKaharay | last post by:
I am using the code below and I am getting this error: "Insufficient state to deserialize the object. More information is needed." Has anyone seen this? It is working with other dll's but not on a...
1
by: Christopher Dedels | last post by:
Is it possible to deserialize an object which was serialized in c/c++? If so, how? Regards, Chris
3
by: Mullin Yu | last post by:
I want to know how can i deserialize an object to a string, instead of a file as below:ShoppingList myList = new ShoppingList(); myList.AddItem( new Item( "eggs",1.49 ) ); myList.AddItem( new...
0
by: Rajesh Cheedalla | last post by:
Hi All, I have a class View, defined with a property called Parameters which is of type class ControlParameters. View class is used serialize/deserialize view.xml. User may define a new class...
3
by: Just D. | last post by:
Can we deserialize an unknown object? The task is simple - we have many different types of objects stored in ArrayList. We can implement Serialize() method for each object and serialize all...
1
by: Julia | last post by:
Hi, I know of course how to deserialize an object,but i wonder if i can deserialize an object from two different XML 1.Do I need to create a formatter for each type of XML? 2.Should I only...
6
by: Redowl | last post by:
Hi, I am having an issue deserializing a business object from a SQL stored procedure. I have created the class using the XSD tool and the resulting XMLDATA schema but when I try to create the...
0
by: anchiang | last post by:
Hi All, I have XML: <RegistryResponse status="Success" xmlns="urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1"> <AdhocQueryResponse xmlns="urn:oasis:names:tc:ebxml-regrep:query:xsd:2.1"> ...
1
by: incunix | last post by:
Trying to run a standalone program I downloaded and know works (I have spoken to the developer on the team etc...). The url where to find it is http://pyxida.sourceforge.net/ I initially...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.