473,549 Members | 2,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1671
On Sun, 08 Jan 2006 18:17:43 GMT, "Chris" <mi*********@gm ail.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******@coldm ail.com> wrote in message
news:il******** *************** *********@4ax.c om...
On Sun, 08 Jan 2006 18:17:43 GMT, "Chris" <mi*********@gm ail.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*********@gm ail.com> wrote in message
news:vW******** ************@fe 1.news.blueyond er.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*********@gm ail.com>
wrote:

"Chris" <mi*********@gm ail.com> wrote in message
news:vW******* *************@f e1.news.blueyon der.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
deserializin g 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
3161
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. It seems that either I can use default serialization or implement ISerializable. Is there any way to do both (e.g. extend the default...
5
2813
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 like it to return an XML serialized version of an object.
6
2705
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 rmi. Just to keep my question short I am posting trimmed version of my code. //file: Serializable.cs
1
1910
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 that was generated has the following code at the end. <System.Xml.Serialization.SoapTypeAttribute("Map",
6
6048
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 problem is that in my derived classes, the XML that is automatically generated is lacking the properties of the base class. For example: public class...
0
1797
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 /> <LOB /> <Name> <FirstName />
0
1193
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 site but only basics serialization is there. Topics: 1) serialize a nested object in mutiple files by maintaining the relation on parent level. ...
2
5544
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 I am trying to read from a binary archive using Boost serialization. I am new to this, and it is possible that I have not understood the usage. In...
0
7726
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7967
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5377
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1953
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.