473,761 Members | 10,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Mem oryStream stream)
{
XmlSerializer serializer = new XmlSerializer(t ypeof(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.Dese rialize(stream) ;
}
}

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

Jul 21 '05 #1
4 1826
I would implement a static method in the class that is serializable, like:

public static YourClass Deserialize ( Stream fromStream )
{
XmlSerializer serializer = new XmlSerializer(t ypeof(YourClass ));
return (YourClass) serializer.Dese rialize(stream) ;
}
"Bob Rock" <no************ *************** @hotmail.com> wrote in message
news:ug******** ********@TK2MSF TNGP12.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(Mem oryStream stream)
{
XmlSerializer serializer = new XmlSerializer(t ypeof(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.Dese rialize(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******** **********@news 2.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(t ypeof(YourClass ));
return (YourClass) serializer.Dese rialize(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(t ypeof(YourClass )).Deserialize( stream);
this.aProperty = c.aProperty;
this.someOtherP roperty = c.someOtherProp erty;
c = null;
}

Regards, Dennis

"Bob Rock" <no************ *************** @hotmail.com> wrote in message
news:ey******** ******@TK2MSFTN GP10.phx.gbl...
"Dennis Myrén" <de****@oslokb. no> wrote in message
news:AB******** **********@news 2.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(t ypeof(YourClass ));
return (YourClass) serializer.Dese rialize(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******** ******@TK2MSFTN GP10.phx.gbl...
"Dennis Myrén" <de****@oslokb. no> wrote in message
news:AB******** **********@news 2.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(t ypeof(YourClass ));
return (YourClass) serializer.Dese rialize(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
10849
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 absolute file name of a .class file eg. C:\myJava\myApp.java, then how do i translate this file name to a java class name the Object.forName method would accept has it's parameter? thanks,
1
10957
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 object's members. now, i have an enclosing class, which has a static factory method (loads an instance of the enclosing class from a serialized file, and returns it). during the loading, some initialization takes place. one of the initialization...
4
1591
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 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:
2
1239
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 when selecting the Help command from the Help menu. Can someone give me an example of the code that should follow the MenuItem_Click event in order to activate the Help. With thanks
11
4226
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 database. The problem is, when I was debugging the application, it doesn't show the form. It just pass through the ShowDialog() method, and my flag confirm becomes
2
2019
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 installed depending on each user's requirements. I'd like to implement a function in one plugin that only executes if another plugin is present. Typically, for one plugin to access a second plugin, I need to set a reference at design-time to the...
3
6508
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 situation boiled down to the nuts and bolts... 1. Create a simple web application (not a web site) in VS 2005 2. Add a web.config and then set the Session to InProc and cookieless="Autodetect" <sessionState mode="InProc" cookieless="AutoDetect"...
5
2483
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 stories go, a guy created a GUI as a macro using VB and saved everything under the shared folder for our clients' server, which then was deleted. Since he saved it as a macro it saved the .rsf file - but did not save any of the modules, or forms, that...
7
6029
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. Therefore the textbox is actually empty when the Page_Load event fires. Is there a way to execute the method after the javascript has run and
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.