471,325 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

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 7614
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Serg | last post: by
2 posts views Thread by Snowman | last post: by
3 posts views Thread by Amadelle | last post: by
3 posts views Thread by AnkitAsDeveloper [Ankit] | last post: by
3 posts views Thread by parrot toes | last post: by
5 posts views Thread by Greg Allen | last post: by

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.