473,626 Members | 3,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialization of a complicated class

Hi,

I need to write a serialization (to XML string) and restoring (from XML
string) of a very complicated object. The object uses a few classes, one
class has two ArrayLists, etc. The general structure supposes to have 2-3
levels.

What's easier, to write a serialization method for each class used by the
main class and a short method to gather all these XML strings together into
one complex string or to use some trick and serialize the whole structure
into one XML?

I expect that this structure will be changing and the deserialization should
read all previous versions, so that Ser./Deser. methods should be created
now to support all further versions.

Did anybody write these methods for a very complicated class? What's
experience?

Thanks,
Dmitri

Nov 16 '05 #1
2 2501
You could try using SOAP to serialize the class to see if it meets your
needs.

To serialize to soap you first need to add a reference to the SOAP formatter
(System.Runtime .Serialization. Formatters.Soap ).

The use some code like this:

SoapFormatter form = new SoapFormatter() ;
form.Serialize( st, myInstance);

Where st is a stream (a file stream, or memory stream or something), and
myInstance is the instance of your object.

Then to deserialize the object, use something like:

myInstace = (MyClass) form.Deserializ e(st);

SOAP generally works quite well with version changes.

Hope that helps.
John

"Just D" <no@spam.please > wrote in message
news:By6Bc.2066 $DQ4.706@fed1re ad05...
Hi,

I need to write a serialization (to XML string) and restoring (from XML
string) of a very complicated object. The object uses a few classes, one
class has two ArrayLists, etc. The general structure supposes to have 2-3
levels.

What's easier, to write a serialization method for each class used by the
main class and a short method to gather all these XML strings together into one complex string or to use some trick and serialize the whole structure
into one XML?

I expect that this structure will be changing and the deserialization should read all previous versions, so that Ser./Deser. methods should be created
now to support all further versions.

Did anybody write these methods for a very complicated class? What's
experience?

Thanks,
Dmitri

Nov 16 '05 #2
You can instruct the XML Serializer how to translate your properties into XML using attributes:

[Serializable]
public class Order
{

public Order() //parameterless constructor is needed for deserialization
{
}

[XmlAttribute("D ate")]
public string Date
{ get{ .... } set{....}}

private ArrayList mLines = new ArrayList();
[XmlArray("Lines ")]
[XmlArrayElement ("Line",typeof( OrderLine))]
public IList OrderLines
{
get
{
return mLines;
}
}


}

If you add new properties to your class, the deserializer will still be able to read old files.
These properties will retain their default value.
If you remove properties, the deserializer will ignore the XML nodes he doesn't recognize.
Just make sure you don't change the return types of properties, because then you will get exceptions.

Regards,
Marc Selis
"Just D" <no@spam.please > wrote in message news:By6Bc.2066 $DQ4.706@fed1re ad05...
Hi,

I need to write a serialization (to XML string) and restoring (from XML
string) of a very complicated object. The object uses a few classes, one
class has two ArrayLists, etc. The general structure supposes to have 2-3
levels.

What's easier, to write a serialization method for each class used by the
main class and a short method to gather all these XML strings together into
one complex string or to use some trick and serialize the whole structure
into one XML?

I expect that this structure will be changing and the deserialization should
read all previous versions, so that Ser./Deser. methods should be created
now to support all further versions.

Did anybody write these methods for a very complicated class? What's
experience?

Thanks,
Dmitri

Nov 16 '05 #3

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

Similar topics

37
4985
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
3
3169
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 serialization). In other words, I want to be able to implement my custom serialization code but call the...
6
2709
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
3
2451
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type System.Collections.Comparer has 1 members, number of members deserialized is 0. at System.Runtime.Serialization.Formatters.Binary.ReadObjectInfo.GetMemberTypes(String
27
1934
by: Codemonkey | last post by:
Heya All, Sorry, but I think it's about time for a monkey-ramble. I've just had enough of trying to serialize even simple objects with VB. A simple task you may think - stick the <Serialized()> attribute on the class and away you go. As Homer would say - "D'Oh" The root of my problem lies in the way VB implements Events and the fact that you can't apply the <NonSerialized> attribute to the little rascals.
10
1477
by: SStory | last post by:
My app is near completed for the basic feature of version 1.0. I have an extensive object model and I now want to persist my objects using serialization. I have chosen binaryformatter to serialize, and custom serialization, which I understand will allow me the flexibility of not breaking old things when I add members to classes in the future and send to existing customers. 1.) is there anything else to consider with the custom...
11
3656
by: William | last post by:
I'm looking for an example that would show how to serialize a c++ object at it's simplest w/o using any other api's. I have a class that I want to serialize and then pass to my obj-c class so I can send it over the wire. I'm just looking for how to serialize it, then pack it back up on the other end. Any help much appreciated.
0
6599
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an object and transport it over the Internet using HTTP between a client and a server. On the other end, deserialization reconstructs the object from the stream. XML serialization serializes only the public fields and property values of an object...
2
5556
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 the code below, the string "faultblock" seems to be causing the problem. The code crashes in the ia...
0
8265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8196
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7193
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5574
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4197
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.