471,337 Members | 848 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Beginner xml serialization question

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.
Jan 8 '06 #1
4 1578
On Sun, 08 Jan 2006 18:17:43 GMT, "Chris" <mi*********@gmail.com>
wrote:
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

--

The ultimate truth is that there is no ultimate truth
Jan 9 '06 #2

"rossum" <ro******@coldmail.com> wrote in message
news:il********************************@4ax.com...
On Sun, 08 Jan 2006 18:17:43 GMT, "Chris" <mi*********@gmail.com>
wrote:

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
Jan 9 '06 #3

"Chris" <mi*********@gmail.com> wrote in message
news:vW********************@fe1.news.blueyonder.co .uk...

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
Jan 9 '06 #4
On Mon, 09 Jan 2006 04:52:33 GMT, "Chris" <mi*********@gmail.com>
wrote:

"Chris" <mi*********@gmail.com> wrote in message
news:vW********************@fe1.news.blueyonder.c o.uk...

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

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

--

The ultimate truth is that there is no ultimate truth
Jan 9 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Aaron Clamage | last post: by
5 posts views Thread by francois | last post: by
6 posts views Thread by Uttam | last post: by
6 posts views Thread by John Glover | last post: by
reply views Thread by rosydwin | last post: by

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.