473,513 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XML Deserialization

Is there a way to be notified when the deserialization of an XML is
complete, like a callback method?

I tried the IDeserializationCallback interface, but that doesnt for
XML-Deserialization.

thanks
josh

Dec 15 '05 #1
12 7742
Use a synchronous overload of the XmlSerializer.Deserialize method; when it
returns, it is finished.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"yoshijg" <jo*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Is there a way to be notified when the deserialization of an XML is
complete, like a callback method?

I tried the IDeserializationCallback interface, but that doesnt for
XML-Deserialization.

thanks
josh

Dec 15 '05 #2
Hi Kev,

Do you mean something like this...

--
FooType fooobj;
XmlSerializer xmlObj = new System.Xml.XmlSerialialzer(TypeOf(FooType));
Filestream strObj = new Filestream("foo", Open);
fooobj = xmlObj.Deserialize( strObj );
fooObj.DeserializeComplete(); //Notify manually
--

Yeah, i am doing that now... but I want the object being created inside
of the "Deserialize" method to be notified when it's done being created
by the "XmlSerializer".

I can do what you suggest and it's easy...but some coder may forget to
call "DeserializeComplete". Now I can wrap this whole thing in a
function call, but that's just another hoop everybody doesnt need to
jump through.

Now why doesn't implementing IDeserializationCallback work?

Dec 15 '05 #3
I don't know why. How are you implementing it?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"yoshijg" <jo*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi Kev,

Do you mean something like this...

--
FooType fooobj;
XmlSerializer xmlObj = new System.Xml.XmlSerialialzer(TypeOf(FooType));
Filestream strObj = new Filestream("foo", Open);
fooobj = xmlObj.Deserialize( strObj );
fooObj.DeserializeComplete(); //Notify manually
--

Yeah, i am doing that now... but I want the object being created inside
of the "Deserialize" method to be notified when it's done being created
by the "XmlSerializer".

I can do what you suggest and it's easy...but some coder may forget to
call "DeserializeComplete". Now I can wrap this whole thing in a
function call, but that's just another hoop everybody doesnt need to
jump through.

Now why doesn't implementing IDeserializationCallback work?

Dec 16 '05 #4
Hey kev,

Taking from the example above...

The only thing to add/note is that FooType implements the
IDeserializationCallback , and the call to deserialize looks like

FooType fooobj;
XmlSerializer xmlObj = new System.Xml.XmlSerialialzer(TypeOf(FooType));
Filestream strObj = new Filestream("foo", Open);
fooobj = xmlObj.Deserialize( strObj );
---------------

Note, I am not calling OnDeserialization manually like above.

Dec 16 '05 #5
What I meant was, how are you implementing IDeserializationCallback? The
code you just posted doesn't do this. Your question was "why doesn't it
work?" In order to see why it doesn't work, I have to see how you're using
it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"yoshijg" <jo*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hey kev,

Taking from the example above...

The only thing to add/note is that FooType implements the
IDeserializationCallback , and the call to deserialize looks like

FooType fooobj;
XmlSerializer xmlObj = new System.Xml.XmlSerialialzer(TypeOf(FooType));
Filestream strObj = new Filestream("foo", Open);
fooobj = xmlObj.Deserialize( strObj );
---------------

Note, I am not calling OnDeserialization manually like above.

Dec 16 '05 #6
/*
Serializes/Deserializes properly as expected. But doesnt call the
OnDeserialization();
*/

using System;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;

namespace Test
{
public class Class1
{
[STAThread]
static void Main(string[] args)
{
//Serialize to disk first
Foobar fooObj = new Foobar();
XmlSerializer xml = new XmlSerializer(typeof(Foobar));
StreamWriter ios = new StreamWriter("foobar.xml");
xml.Serialize(ios, fooObj);
ios.Close();

//Deserialize from disk
Foobar fooObj2 = null;
XmlSerializer xml2 = new XmlSerializer(typeof(Foobar));
FileStream ios2 = new FileStream("foobar.xml",
FileMode.Open);
fooObj2 = (Foobar)xml.Deserialize(ios2);
ios2.Close();

}
}

[Serializable()]
public class Foobar
: IDeserializationCallback
{
private string m_Foo = "fooooo";

public string FooProperty
{
get
{
return m_Foo;
}
set
{
m_Foo = value;
}
}

public void OnDeserialization(Object sender)
{
Console.WriteLine("Deserialization complete");
}
}
}

Dec 17 '05 #7
/*
Serializes/Deserializes properly as expected. But doesnt call the
OnDeserialization();
*/

using System;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;

namespace Test
{
public class Class1
{
[STAThread]
static void Main(string[] args)
{
//Serialize to disk first
Foobar fooObj = new Foobar();
XmlSerializer xml = new XmlSerializer(typeof(Foobar));
StreamWriter ios = new StreamWriter("foobar.xml");
xml.Serialize(ios, fooObj);
ios.Close();

//Deserialize from disk
Foobar fooObj2 = null;
XmlSerializer xml2 = new XmlSerializer(typeof(Foobar));
FileStream ios2 = new FileStream("foobar.xml",
FileMode.Open);
fooObj2 = (Foobar)xml.Deserialize(ios2);
ios2.Close();

}
}

[Serializable()]
public class Foobar
: IDeserializationCallback
{
private string m_Foo = "fooooo";

public string FooProperty
{
get
{
return m_Foo;
}
set
{
m_Foo = value;
}
}

public void OnDeserialization(Object sender)
{
Console.WriteLine("Deserialization complete");
}
}
}

Dec 17 '05 #8
Well, you havent implemented the interface. See the following MSDN article:

http://msdn.microsoft.com/library/de...ationTopic.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"yoshijg" <jo*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
/*
Serializes/Deserializes properly as expected. But doesnt call the
OnDeserialization();
*/

using System;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;

namespace Test
{
public class Class1
{
[STAThread]
static void Main(string[] args)
{
//Serialize to disk first
Foobar fooObj = new Foobar();
XmlSerializer xml = new XmlSerializer(typeof(Foobar));
StreamWriter ios = new StreamWriter("foobar.xml");
xml.Serialize(ios, fooObj);
ios.Close();

//Deserialize from disk
Foobar fooObj2 = null;
XmlSerializer xml2 = new XmlSerializer(typeof(Foobar));
FileStream ios2 = new FileStream("foobar.xml",
FileMode.Open);
fooObj2 = (Foobar)xml.Deserialize(ios2);
ios2.Close();

}
}

[Serializable()]
public class Foobar
: IDeserializationCallback
{
private string m_Foo = "fooooo";

public string FooProperty
{
get
{
return m_Foo;
}
set
{
m_Foo = value;
}
}

public void OnDeserialization(Object sender)
{
Console.WriteLine("Deserialization complete");
}
}
}

Dec 17 '05 #9
Hey Kev,

Thanks for the article. I guess IDeserializationCallback only works
with the "BinaryFormatter", which really seems counter intuitive.
Don't you? Wouldn't you expect a Callback to occur for any type of
Deserialization.
josh

Dec 18 '05 #10
To be honest, josh, I don't understand why you want to do this. Your
original message said that you want the object being created by the
XmlSerializer to be notified that it is done being created. Well, that's
like trying to notify someone that they've woken up. Until they wake up,
they're asleep. Until the object is done being created, it doesn't exist.
You don't even need to raise an event at all.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"yoshijg" <jo*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hey Kev,

Thanks for the article. I guess IDeserializationCallback only works
with the "BinaryFormatter", which really seems counter intuitive.
Don't you? Wouldn't you expect a Callback to occur for any type of
Deserialization.
josh

Dec 18 '05 #11
Hey Kev,

First ... I just want to say I appreciate your feed back on this. I
truly enjoy hearing other people's perspective on programming.

I am not trying to notify the object that they have "woken up", so I
agree with you on that. What I am trying to do is finish the creation
of the object by filling in the holes during the "OnDeserialization"
method, because that is the last method called before the object is
returned to the user. Kind of like the "Render" method in "asp.net" it
is the last point where you can alter the html before it is sent to the
client/browser.

You may still not see eye to eye with me on this, but there are
situations where I cant serialize everything to disk. For example, I
can't serialize a password field straight on to disk, but I can write
the "encrypted" value in place of it. Then during deserialization I
need to decrypt the value back to it's original form. Doing that type
of "clean up" in the "OnDeserialization" method would be an ideal
location ... right?
This is just an example I thought of now, so I am sure there are other
ways to solve it.

"Callbacks" / notifications occur throughout the framework. I just
find it weird and inconsistent that the "IDeserializationCallback"
would work for "BinaryFormatter" but not for "Xml" format.

So... If you dont see the need for callbacks on deserialization, then
can you answer why the "IDeserializationCallback" exists? and.. Why
does it work for "BinaryFormatter", but not for the "XmlSerializer" ?

I have some theories on this, but they are just theories after all.

Thanks kev.

peace,
josh

Dec 18 '05 #12
Hi josh,

We don't need to argue the point as to whether or not this is necessary or a
good idea. I'll just take you at your word that you want this, regardless.
:-)

After a good bit of research, both into Binary Serialization and into XML
Serialization, I can only offer guesses as to why the BinaryFormatter raises
this event, but the XmlSerializer does not. Binary Serialization is often
used with Remoting, and is used when performance and latency are more of an
issue than when using XML Serialization. After all, XML Serialization is
generally used locally, or via Web Services. With Web Services, latency is
expected. Again, I'm just guessing here. With Binary Serialization, it is
possible to serialize every member of a class, both data and process, but at
a cost. So, from the examples I've seen, the IDeserializationCallback was
used to allow the deserialized class to reconstruct members that were not
serialized, with the justification being that performance was gained in not
serializing these members.

Now, I suppose it is conceivable that one might want to do something like
this with XML Serialization. For whatever reasons, it just doesn't exist in
the System.Xml.Serialization NameSpace. What you can do is to implement your
own class(es) that do this, if you like. Otherwise, you may as well stick
with what you're presently working with.

Sorry I couldn't be more help.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

"yoshijg" <jo*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hey Kev,

First ... I just want to say I appreciate your feed back on this. I
truly enjoy hearing other people's perspective on programming.

I am not trying to notify the object that they have "woken up", so I
agree with you on that. What I am trying to do is finish the creation
of the object by filling in the holes during the "OnDeserialization"
method, because that is the last method called before the object is
returned to the user. Kind of like the "Render" method in "asp.net" it
is the last point where you can alter the html before it is sent to the
client/browser.

You may still not see eye to eye with me on this, but there are
situations where I cant serialize everything to disk. For example, I
can't serialize a password field straight on to disk, but I can write
the "encrypted" value in place of it. Then during deserialization I
need to decrypt the value back to it's original form. Doing that type
of "clean up" in the "OnDeserialization" method would be an ideal
location ... right?
This is just an example I thought of now, so I am sure there are other
ways to solve it.

"Callbacks" / notifications occur throughout the framework. I just
find it weird and inconsistent that the "IDeserializationCallback"
would work for "BinaryFormatter" but not for "Xml" format.

So... If you dont see the need for callbacks on deserialization, then
can you answer why the "IDeserializationCallback" exists? and.. Why
does it work for "BinaryFormatter", but not for the "XmlSerializer" ?

I have some theories on this, but they are just theories after all.

Thanks kev.

peace,
josh

Dec 18 '05 #13

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

Similar topics

2
2344
by: Serg | last post by:
Hi. I have a problem with deserialization decimal value. I have a simple class: public class A { public decimal d; } The serialization a value 0.00000001 is OK. The file is
3
1643
by: TEK | last post by:
There seems to be a bug when deserialization some classes in the .NET framework. If you try to deserialize a class that has a base class that holds a struct with a member that is implementing...
2
3819
by: Snowman | last post by:
Suppose I have a RootObject which holds a collection of other objects. The other objects have a property (Parent) which refers back to the "parent" collection (b.t.w. my collection is based on...
3
9784
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database...
3
2052
by: AnkitAsDeveloper [Ankit] | last post by:
Hi i am serializing a 'ref struct' object as follows : private: void Seri( String ^path, Object^ obj ) { FileStream^ fileStrm ; try { //Serialize entire object into Binary stream
3
9775
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the...
1
3717
by: parrot toes | last post by:
I tried to post this question before, but there was an error when posting. I case it did get posted and in order to avoid duplication, I'll just repost a summary. I have written a dotnet client...
8
3445
by: ashoksrini | last post by:
Hi All, I have the below requirement and would like to get some feeback from the group on the best way to implement: 1. I have WSDL defined exposing few web services. 2. We dont have a...
5
2278
by: Greg Allen | last post by:
I am consuming a web service and using the generated Reference.cs to access the service and the objects associated with it. I have run into a problem where some inherited classes are not being...
7
16125
by: Andrew | last post by:
Hi, I am using DataContractJsonSerializer to deserialize JSON string in C# objects but I am having a problem. Suppose I have a class: class Item { public ItemId Id { get; set; }
0
7160
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...
0
7537
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...
0
7525
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...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1594
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
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...

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.