Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XmlSerializer

0 views
Skip to first unread message

Duggi

unread,
Sep 9, 2008, 2:22:26 PM9/9/08
to
Hi

In the following code why it is required to type cast while de-
serialize... any ideas???

to create an object of XmlSerializer, Type is a compalsary parameter.
Atleast by default XmlSerializer should convert it to the Type it is
made for...

class MyClass
{
//Some implementation
}

class Program
{

static void Main(string[] args)
{
MyClass cs;
XmlSerializer s = new XmlSerializer(typeof(MyClass));
FileStream fs = new FileStream("serializedobject.xml",
FileMode.Open);

cs = (MyClass) s.Deserialize(fs);


Console.ReadLine();
}

-Cnu

Peter Duniho

unread,
Sep 9, 2008, 3:07:42 PM9/9/08
to
On Tue, 09 Sep 2008 11:22:26 -0700, Duggi <DuggiSri...@gmail.com>
wrote:

> In the following code why it is required to type cast while de-
> serialize... any ideas???
>
> to create an object of XmlSerializer, Type is a compalsary parameter.
> Atleast by default XmlSerializer should convert it to the Type it is
> made for...

You seem to be confusing the issue of the type as the compiler knows it
with the issue of the type as the run-time knows it.

You need to cast because the _compiler_ doesn't know the type. But the
cast doesn't do anything to the object at all. The serializer _does_
create an instance of the desired type, as you expect. All the cast does
is tell the compiler what type it is, so that you can use the instance as
the actual type rather than Object (*).

If XmlSerializer was a generic type, then it would have been possible for
the Deserialize() method to simply return instance as the correct type.
But the class pre-dates generics and so only the casting approach was
possible when the class was designed.

Pete

(*) I've oversimplified casting a bit. The run-time actually checks the
type and will throw an exception if it's wrong. But, from a practical
point of view, the main purpose for casting is to translate for the
compiler how a given instance is perceived. It doesn't change the
instance itself in any way.

sloan

unread,
Sep 9, 2008, 5:15:29 PM9/9/08
to
Check here for some ideas/samples:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!114.entry

I serialize and deserialize from a file....


"Duggi" <DuggiSri...@gmail.com> wrote in message
news:d8b26805-50ae-4498...@v16g2000prc.googlegroups.com...

Duggi

unread,
Sep 10, 2008, 1:58:37 AM9/10/08
to
On 10 Sep, 02:15, "sloan" <sl...@ipass.net> wrote:
> Check here for some ideas/samples:http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!114.entry
>
> I serialize and deserialize from a file....
>
> "Duggi" <DuggiSrinivasa...@gmail.com> wrote in message

>
> news:d8b26805-50ae-4498...@v16g2000prc.googlegroups.com...
>
> > Hi
>
> > In the following code why it is required to type cast while de-
> > serialize... any ideas???
>
> > to create an object of XmlSerializer, Type is a compalsary parameter.
> > Atleast by default XmlSerializer should convert it to the Type it is
> > made for...
>
> > class MyClass
> > {
> >        //Some implementation
> > }
>
> > class Program
> > {
>
> >        static void Main(string[] args)
> >        {
> >            MyClass cs;
> >            XmlSerializer s = new XmlSerializer(typeof(MyClass));
> >            FileStream fs = new FileStream("serializedobject.xml",
> > FileMode.Open);
>
> >            cs = (MyClass) s.Deserialize(fs);
>
> >            Console.ReadLine();
> >        }
>
> > -Cnu

That is gre8 techie to know... Thanks for sharing

-Cnu

Duggi

unread,
Sep 10, 2008, 2:04:22 AM9/10/08
to
On 10 Sep, 00:07, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> wrote:
> On Tue, 09 Sep 2008 11:22:26 -0700, Duggi <DuggiSrinivasa...@gmail.com>  

Pete,
Thanks for the reply.

> You need to cast because the _compiler_ doesn't know the type.

I did not know it. I was under the impression that ... While creating
an object (s) it is required to specify the Type(MyClass) of object
for which serialization object (s) is created. So logically, compiler
should know that when ever "s" is used it has to deserialize to
"MyClass".

This is what exactly my confusion was.

Thanks
-Cnu


Marc Gravell

unread,
Sep 10, 2008, 2:09:50 AM9/10/08
to
To do this it would have to use generics i.e. it would be
XmlSerializer<Foo>, not new XmlSerializer(typeof(Foo)).

In theory this would be quite nice, but a: XmlSerializer pre-dates
generics (it exsisted with 1.1), and b: it would be [slightly] harder
to use in certain scenarios where the type isn't known (you'd have to
use MakeGenericType etc, although presumably this could have been
wrapped).

The first seems a stronger reason, which makes me wonder why
DataContractSerializer isn't generic... we may never know ;-p But you
only need to cast, so it isn't really much of an issue.

Marc

Peter Duniho

unread,
Sep 10, 2008, 2:46:47 AM9/10/08
to
On Tue, 09 Sep 2008 23:04:22 -0700, Duggi <DuggiSri...@gmail.com>
wrote:

> [...]


>> You need to cast because the _compiler_ doesn't know the type.
>
> I did not know it. I was under the impression that ... While creating
> an object (s) it is required to specify the Type(MyClass) of object
> for which serialization object (s) is created. So logically, compiler
> should know that when ever "s" is used it has to deserialize to
> "MyClass".

The compiler doesn't know that. All it knows is that "typeof(MyClass)"
was passed to the constructor of XmlSerializer. It has no idea what
XmlSerializer is going to do with that parameter. So, no...it's not true
that the "compiler should know...it has to deserialize to 'MyClass'". The
compiler's not even aware that deserialization is what's happening. All
it knows is that you've created an object by passing in a specific
parameter, and then called a method on that object. It has no specific
knowledge of what's actually going on.

Pete

Duggi

unread,
Sep 10, 2008, 3:36:26 AM9/10/08
to

Thanks for the reply Marc. I think I got the zest of it.

-Cnu

Duggi

unread,
Sep 10, 2008, 3:36:37 AM9/10/08
to
On Sep 10, 11:46 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
> On Tue, 09 Sep 2008 23:04:22 -0700, Duggi <DuggiSrinivasa...@gmail.com>  

Thanks for the reply Pete. I think I got the zest of it.

0 new messages