PC Review


Reply
Thread Tools Rate Thread

Deserialize method that "loads" the class instance itself: how???

 
 
Bob Rock
Guest
Posts: n/a
 
      14th May 2004
Hello,

I've got an xml stream that I'd need to deserialize into an instance of a
given class A. I'd like to create an instance method on class A (method
Deserialize) that takes this XML stream as input and deserializes it "into
itself" ... in other words I'd like it to "fill" the instance of class A on
which the method has been called instead of returning another instance of
class A.

The code below gives a good idea of what I'd like:

public class A
{
public void Deserialize(MemoryStream stream)
{
XmlSerializer serializer = new XmlSerializer(typeof(A));

// the following line of code obviously does not work since this is
readonly
// but it gives a good idea of what I'd like to do
this = serializer.Deserialize(stream);
}
}

How can I accomplish this without having to manually "load" all the class
fields???


Bob Rock



 
Reply With Quote
 
 
 
 
Dennis Myrén
Guest
Posts: n/a
 
      14th May 2004
I would implement a static method in the class that is serializable, like:

public static YourClass Deserialize ( Stream fromStream )
{
XmlSerializer serializer = new XmlSerializer(typeof(YourClass));
return (YourClass) serializer.Deserialize(stream);
}


"Bob Rock" <(E-Mail Removed)> wrote in message
news:ug6R%(E-Mail Removed)...
> Hello,
>
> I've got an xml stream that I'd need to deserialize into an instance of a
> given class A. I'd like to create an instance method on class A (method
> Deserialize) that takes this XML stream as input and deserializes it "into
> itself" ... in other words I'd like it to "fill" the instance of class A

on
> which the method has been called instead of returning another instance of
> class A.
>
> The code below gives a good idea of what I'd like:
>
> public class A
> {
> public void Deserialize(MemoryStream stream)
> {
> XmlSerializer serializer = new XmlSerializer(typeof(A));
>
> // the following line of code obviously does not work since this

is
> readonly
> // but it gives a good idea of what I'd like to do
> this = serializer.Deserialize(stream);
> }
> }
>
> How can I accomplish this without having to manually "load" all the class
> fields???
>
>
> Bob Rock
>
>
>



 
Reply With Quote
 
Bob Rock
Guest
Posts: n/a
 
      14th May 2004
"Dennis Myrén" <(E-Mail Removed)> wrote in message
news:AB2pc.1462$(E-Mail Removed)...
> I would implement a static method in the class that is serializable, like:
>
> public static YourClass Deserialize ( Stream fromStream )
> {
> XmlSerializer serializer = new XmlSerializer(typeof(YourClass));
> return (YourClass) serializer.Deserialize(stream);
> }


Dannis, that is what I will do if I can't find an easy way to do it with an
instance method.

Bob Rock



 
Reply With Quote
 
Dennis Myrén
Guest
Posts: n/a
 
      14th May 2004
Well, you can do it with an instance method by deserializing the XML
to a new instance of the class, and than copy all properties of that
instance to your actual instance, like;

public void LoadState ( Stream fromStream )
{
YourClass c = (YourClass) new
XmlSerializer(typeof(YourClass)).Deserialize(stream);
this.aProperty = c.aProperty;
this.someOtherProperty = c.someOtherProperty;
c = null;
}

Regards, Dennis

"Bob Rock" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Dennis Myrén" <(E-Mail Removed)> wrote in message
> news:AB2pc.1462$(E-Mail Removed)...
> > I would implement a static method in the class that is serializable,

like:
> >
> > public static YourClass Deserialize ( Stream fromStream )
> > {
> > XmlSerializer serializer = new XmlSerializer(typeof(YourClass));
> > return (YourClass) serializer.Deserialize(stream);
> > }

>
> Dannis, that is what I will do if I can't find an easy way to do it with

an
> instance method.
>
> Bob Rock
>
>
>



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      14th May 2004
Bob,

Why not do this. Get the schema for the XML and then run the XSD.exe
tool against it. This will create a C# file which is a class (which might
derive from DataSet if you say so) that you can compile in your app. Then,
you can use the ReadXml method on the DataSet (if you choose to have your
class derive from that), or the Deserialize method on the XmlSerializer
class to deserialize an instance of the class in your app.

Once you have that, you can do anything you want with the class, or have
a containing class use it for whatever purposes you wish.

Hope this helps.



"Bob Rock" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Dennis Myrén" <(E-Mail Removed)> wrote in message
> news:AB2pc.1462$(E-Mail Removed)...
> > I would implement a static method in the class that is serializable,

like:
> >
> > public static YourClass Deserialize ( Stream fromStream )
> > {
> > XmlSerializer serializer = new XmlSerializer(typeof(YourClass));
> > return (YourClass) serializer.Deserialize(stream);
> > }

>
> Dannis, that is what I will do if I can't find an easy way to do it with

an
> instance method.
>
> Bob Rock
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"PasteSpecial method of Range class failed" occurs in one instance of Excel but not another Andrew Microsoft Excel Discussion 1 8th Jul 2004 12:17 PM
<FORM METHOD="post" onSubmit="return fieldcheck()" name="orientation" action="http://ws-kitty.BU.edu/AT/survey/orientation/script/write.asp" language="JavaScript"> Joeyej Microsoft ASP .NET 0 4th Jun 2004 08:55 PM
Deserialize method that "loads" the class instance itself: how??? Bob Rock Microsoft Dot NET Framework 5 14th May 2004 02:46 PM
Deserialize method that "loads" the class instance itself: how??? Bob Rock Microsoft C# .NET 4 14th May 2004 02:30 PM
Howto customize "Add Method" wizard on "Add Method" menu of Class View? Bill Smarty Microsoft Dot NET Framework 0 13th Jan 2004 06:54 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 PM.