473,405 Members | 2,310 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,405 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

Jul 21 '05 #1
4 1799
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

Jul 21 '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

Jul 21 '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

Jul 21 '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

Jul 21 '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....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.