473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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

Nov 16 '05 #1
4 1580
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" <no***************************@hotmail.com> wrote in message
news:ug****************@TK2MSFTNGP12.phx.gbl...
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

Nov 16 '05 #2
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:AB******************@news2.e.nsc.no...
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

Nov 16 '05 #3
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(strea m);
this.aProperty = c.aProperty;
this.someOtherProperty = c.someOtherProperty;
c = null;
}

Regards, Dennis

"Bob Rock" <no***************************@hotmail.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:AB******************@news2.e.nsc.no...
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

Nov 16 '05 #4
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" <no***************************@hotmail.com> wrote in message
news:ey**************@TK2MSFTNGP10.phx.gbl...
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:AB******************@news2.e.nsc.no...
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

Nov 16 '05 #5

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

Similar topics

8
by: Fu Bo Xia | last post by:
the java.lang.Object.forName method takes a java class name and returns a Class object associated with that class. eg. Class myClass = Object.forName("java.lang.String"); by if i only know the...
1
by: Murat Tasan | last post by:
hi, i am having a small problem with constructing an inner class. i am using an inner class (and not a static nested class) because the methods of the inner class need access to the enclosing...
4
by: Bob Rock | last post by:
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...
2
by: Devlei | last post by:
I have added a HelpProvider to my Form, and set the HelpNavigator and HelpKeyword in the Form's properties. When my Form opens and I press F1, the Help activates correctly, but nothing occurs...
11
by: osmarjunior | last post by:
I have a sequence of commands like this: Form1 frm = new Form1(); frm.LoadData(); Boolean confirm = (frm.ShowDialog() == DialogResult.OK); The LoadData method loads information from...
2
by: Steve | last post by:
Kind of a strange question... I have a VB.NET 2.0 solution containing a main project (my EXE) and a number of other projects (class DLLs) that are "plug-ins" to the main app. These plugins get...
3
by: =?Utf-8?B?bWdvcHBlcnQ=?= | last post by:
I'm having an issue with using web services in a web application marked with session cookieless attribuet set to "AutoDetect". My real life scenario is too complex to lay out but here's my...
5
by: solrium | last post by:
Good morning, Thanks in advanced.... and Here are the basics: VB 6.3 on W2K, Using for WRQ Reflections for IBM (v 13.0) Visual Basic Editor (built into the above mainframe) - As most...
7
by: jphaycock | last post by:
Hi all I am calling a method in a Page_Load event that makes use of a value from a text box. Unfortunately the textbox value is only set after a javascript runs and the page is rendered....
7
by: Robert | last post by:
Thanks George, I really am grateful for attempts to be helpful, but this really doesn't answer the question in my OP. What I am looking for is an explanation of WHY things are this way (I was...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.