Beginner xml serialization question

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi all,

I can serialize and deserialize a single class, but I'm wondering if it's
possible to serialize/deserialize different class types in the same file?
For example, I have a base class, and 3 classes derived from it. Instances
of these 3 classes are stored in a List<baseclass>. I'd like to be able to
save the objects in List<>, and later reload them. Is this possible? I can
write each object to the same file by using a different serializer for each
one, but I'm not sure it produces valid xml, and I don't know how to
deserialize them.
Is what I'm trying to do possible? If not how should I go about doing this?

Any help would be greatly appreciated.
 
Hi all,

I can serialize and deserialize a single class, but I'm wondering if it's
possible to serialize/deserialize different class types in the same file?
For example, I have a base class, and 3 classes derived from it. Instances
of these 3 classes are stored in a List<baseclass>. I'd like to be able to
save the objects in List<>, and later reload them. Is this possible? I can
write each object to the same file by using a different serializer for each
one, but I'm not sure it produces valid xml, and I don't know how to
deserialize them.
Is what I'm trying to do possible? If not how should I go about doing this?

Any help would be greatly appreciated.
Since you have derived classes you should try using polymorphism.
Write virtual methods in the base class to serialize and deserialize
the base class. Override those methods in the derived classes to taks
account of the differences in those classes.

This should be covered in any good OOP text.

rossum
 
rossum said:
Since you have derived classes you should try using polymorphism.
Write virtual methods in the base class to serialize and deserialize
the base class. Override those methods in the derived classes to taks
account of the differences in those classes.

This should be covered in any good OOP text.

rossum

Hi rossum,

Could you give any tips on doing this? If I put serialization code in each
derived objects overridden Save() method, because they are of different
types, they each need a serializer of their type don't they?
This creates an XML document where each stored object has a top level
element tag.
Also, how would I go about deserializing a file full of different objects.
Would I need a loop with some form of EOF delimiter? I'm also not sure how
I'd differentiate between each object to pass it to the correct deserializer
for that type.

Perhaps I'm doing this all wrong, but this is all new to me, and everything
I've found through google just deals with the serialization/deserialization
of a single object, never multiple of different types in the same file.

Any help would be appreciated,

Chris
 
Chris said:
Hi rossum,

Could you give any tips on doing this? If I put serialization code in each
derived objects overridden Save() method, because they are of different
types, they each need a serializer of their type don't they?
This creates an XML document where each stored object has a top level
element tag.
Also, how would I go about deserializing a file full of different objects.
Would I need a loop with some form of EOF delimiter? I'm also not sure how
I'd differentiate between each object to pass it to the correct
deserializer for that type.

Perhaps I'm doing this all wrong, but this is all new to me, and
everything I've found through google just deals with the
serialization/deserialization of a single object, never multiple of
different types in the same file.

Any help would be appreciated,

Chris

Ok, I've been playing around and discovered the XmlInclude setting to
include the derived types,. Using this, I've been serializing and
deserializing the List<base> object and all derived types it contains. Is
this how such things are usually done? It seems to be working ok.

Chris
 
Ok, I've been playing around and discovered the XmlInclude setting to
include the derived types,. Using this, I've been serializing and
deserializing the List<base> object and all derived types it contains. Is
this how such things are usually done? It seems to be working ok.

Chris
I can talk about polymorphism, but I am not an expert on XML. You can
always check by reading the XML to see that everything that you expect
is included. If it seems to be working then it probably is. If you
are worried about it then get a colleague to test it for you - a
second pair of eyes often helps find errors.

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top