473,573 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can an object serialize/deserialize itself from XML?

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 SerializationIn fo 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
7 5817
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*******@disc ussions.microso ft.com> wrote in message
news:54******** *************** ***********@mic rosoft.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 SerializationIn fo 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
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
> I was wondering if it possible for an object to serialize/deserialize
itself from XML.

I must be misunderstandin g 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*******@disc ussions.microso ft.com> wrote in message
news:02******** *************** ***********@mic rosoft.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
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
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
> I was wondering if it possible for an object to serialize/deserialize
itself from XML.

I must be misunderstandin g 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*******@disc ussions.microso ft.com> wrote in message
news:02******** *************** ***********@mic rosoft.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
9343
by: Just D. | last post by:
Can we deserialize an unknown object? The task is simple - we have many different types of objects stored in ArrayList. We can implement Serialize() method for each object and serialize all these objects, or maybe even serialize the whole ArrayList at once. Is it possible to deserialize this array list keeping in mind that we can have many...
3
2376
by: Jerry | last post by:
Hi, I have a class like the following: class A { private B _b; A (B b) { _b = b; } ...
7
9549
by: schoenfeld1 | last post by:
I've implemented IPC between two applications using named pipes and binary serialization, but have noticed that the binary formatter is rather slow. It seems that the binary formatter reflects the entire type everytime it is invoked to serialize/deserialize an object of that type. Is there a way to prepare the binary formatter with a...
2
7955
by: ce | last post by:
Being a newbie regarding serialization and memorystreams, I was trying to see if i could improve page performance (avoiding going to the db on a postback) by saving my serialized business object in viewstate and getting it back from the client on a postback. But the last line of the sample code below throws a "Stream was not readable" error...
2
3752
by: Phillip Galey | last post by:
I have an object called Place which contains only string properties and has the <Serializable()> flag before the class name declaration. I also have a collection object called Places, which is implemented using Inherits System.Collections.Specialized.NameObjectCollectionBase and also has the <Serializable()> flag before the class name...
5
2258
by: Matthew | last post by:
I have a nice little Sub that saves data in a class "mySettings" to an XML file. I call it like so: Dim mySettings As mySettings = New mySettings mySettings.value1 = "someText" mySettings.value2 = "otherText" xmlSave("C:\folder\file.xml", mySettings) Here is the sub: Public Shared Sub xmlSave(ByVal path As String, ByVal config As
3
6679
by: GreyAlien007 | last post by:
I extended the class TreeNode to add some properties of my liking. Anyway, no problems there, I can add my derived TreeNode into TreeNodeCollections and use the properties etc. However, when I serialize a class containing my new TreeNode, it doesn't seem to save all the properties back up the inheritance chain. The class that is getting...
11
3647
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.
3
5547
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I am using dotnet remoting with a binarry formatter. I have a property that returns a memorystream that has had a file loaded into it. When I try to access this property though I get an error regarding "the proxy has no channel sink.......or no suitable Client channel to talk to the server."
0
7781
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...
0
8202
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...
1
7788
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...
0
8066
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
6421
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...
0
3733
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...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1304
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1041
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...

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.