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

Can an object serialize/deserialize itself from XML?

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
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.
Thanks for any help

Ian

Jul 21 '05 #2

P: n/a
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.
Jul 21 '05 #3

P: n/a
> 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" <an*******@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com... 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.
Jul 21 '05 #4

P: n/a
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
Jul 21 '05 #5

P: n/a
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.
Nov 22 '05 #6

P: n/a
> 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" <an*******@discussions.microsoft.com> wrote in message
news:02**********************************@microsof t.com... 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.
Nov 22 '05 #7

P: n/a
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
Nov 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.