473,322 Members | 1,409 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,322 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 1655
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
1
by: hl | last post by:
Hi, I'm a beginner and need a little help with getting data back from a web service. I am using VB.Net and have added a web reference to a Wsdl that was provided to me. My reference.vb file...
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
0
by: groovyghoul | last post by:
Hi I have the following XML file: =========================================================== <?xml version="1.0" encoding="UTF-16"?> <Policy xmlns="http://tempuri.org/richard.xsd"> <TransType...
0
by: nobin01 | last post by:
Dear sir; I want ur Help in serialization.I know serialization.I Know binary,soap and xmlserialization also.But i want ur help in following topics.pls help me as soon as possible.I have search in...
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.