XML Deserialization

  • Thread starter Thread starter yoshijg
  • Start date Start date
Y

yoshijg

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
 
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.
 
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?
 
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.
 
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 );
 
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.
 
/*
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");
}
}
}
 
/*
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");
}
}
}
 
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
 
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.
 
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
 
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.
 
Back
Top