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 4 1591
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
"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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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,
|
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...
|
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:
|
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
|
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
| |
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...
|
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"...
|
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...
|
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
|
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
not looking for a work-around).
Again, I am appreciative of the feedback. I will note, that even though I
can use Interfaces, the calling application and the dynamically loaded
assembly both need compile-time refrences to the assembly that...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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...
|
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...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |