Can an object serialize/deserialize itself from XML?

G

Guest

H

I was wondering if it possible for an object to serialize/deserialize itself from XML.

I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a brand new object when deserializing. In my case I have an existing object that I'd like to pass some XML to for the object to repopulate its member variables. Similarly I'd like it to be able to populate an XML string from the values of its member variables. The member variables will be primitive types, plus possibly DateTime values (not sure if that's classed as a primitive type)

Another quirk: not all of the member variables will necessarily be in the XML used to deserialize the object. But the XmlSerializer class seems OK with this

Using the SerializationInfo class seems another option, but I don't know about it taking XML rather than it's own dictionary based data representation

Thanks for any hel

Ian
 
C

Charles Law

Hi Ian

I have tried to do this without luck. The best suggestions seemed to be
either to have a static/shared member function that is called to create the
object which you then assign to your desired object reference, or put all
the persistent stuff into a member class object and call a member function
on the parent to recreate it, if that makes sense.

HTH

Charles


Ian Tompsett said:
Hi

I was wondering if it possible for an object to serialize/deserialize itself from XML.

I'd be guessing that it would need to use the XmlSerializer class, but
that seems to want to create a brand new object when deserializing. In my
case I have an existing object that I'd like to pass some XML to for the
object to repopulate its member variables. Similarly I'd like it to be able
to populate an XML string from the values of its member variables. The
member variables will be primitive types, plus possibly DateTime values (not
sure if that's classed as a primitive type).
Another quirk: not all of the member variables will necessarily be in the
XML used to deserialize the object. But the XmlSerializer class seems OK
with this.
Using the SerializationInfo class seems another option, but I don't know
about it taking XML rather than it's own dictionary based data
representation.
 
G

Guest

Deserialization always creates a new object, but that doesn't mean you can't fake it. I'd use an XmlSerializer, but instead of serializing the entire object, I'd serialize a private member variable called "State" or sumsuch that is a shallow copy of the object. Then, just sync the object with the State variable upon deserialization

That way, it all happens internally and outside viewers of your class would never realize a new object was created.
 
D

Dino Chiesa [Microsoft]

I was wondering if it possible for an object to serialize/deserialize
itself from XML.

I must be misunderstanding the original question, because I use this pattern
in some of the apps I have written.

In my typical scenario, an object has
- a static ReadFromXml() method (or you could call it Deserialize if you
want) which returns an instance of the object. IT's something like a
factory. It accepts an XML file, a stream, or whatever you want to
deserialize from.
- an instance method called Save() which serializes. There might be
multiple Save() methods that serialize to different sinks.

Within these methods, you'll use an XmlSerializer to do the serialization
and de-serialization.
I'd be guessing that it would need to use the XmlSerializer class, but
that seems to want to create a brand new object when deserializing.

When de-serializing, yes, you create a brand new object. This is why the
ReadFromXml is a static method. How can an object instance de-serialize
itself if the instance already exists? In the pattern I have used, the TYPE
can de-serialize itself, but an object instance cannot de-serialize itself
(I think by definition).
In my case I have an existing object that I'd like to pass some XML to for
the object to repopulate its member variables. Similarly I'd like it to be
able to populate an XML string from the values of its member variables. The
member variables will be primitive types, plus possibly DateTime values (not
sure if that's classed as a primitive type).

As some body else suggested, maybe you should just fake it. An instance
method called "RefreshFromXml' might de-serialize a new instance, and then
copy values from the new instance to "this".

-D





John Q said:
Deserialization always creates a new object, but that doesn't mean you
can't fake it. I'd use an XmlSerializer, but instead of serializing the
entire object, I'd serialize a private member variable called "State" or
sumsuch that is a shallow copy of the object. Then, just sync the object
with the State variable upon deserialization.
That way, it all happens internally and outside viewers of your class
would never realize a new object was created.
 
G

Guest

Thanks Dino, John, Charle

Dino, I was hoping the deserialization of an existing object would basically just repopulate the member variables from the XML. In my scenario an object may have a number of member functions that call stored procedures returning XML. This XML contains a serialized representation of the object. So after calling the stored proc I'd like the object to be repopulated

Anyway it sounds from your suggestions I have two options:
- a member object which actually stores the persistent stuff. Properties would then get/set values in this member object.
- deserialize into a new copy of the actual object and then then repopulate member variables out

Thanks for your help, I'll now try and work out which works better for my scenario

Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top