By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,903 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,903 IT Pros & Developers. It's quick & easy.

Serialization of a complicated class

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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.Deserialize(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@fed1read05...
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

P: n/a
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("Date")]
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@fed1read05...
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 discussion thread is closed

Replies have been disabled for this discussion.