473,569 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deserializing an object to itself using Reflection?

I have a class that I am serializing. I put [XmlIgnore] on the properties I
don't want to serialize.

I want to deserialize it to itself like this:

XmlSerializer deserializer = new XmlSerializer(t ypeof(MyClass)) ;
StringReader strReader = new StringReader(xm lData);
XmlReader xmlReader = new XmlTextReader(s trReader);
this = (MyClass)deseri alizer.Deserial ize(xmlReader);

However, you can't assign to "this". It's read only. I know that, so now
I'm creating a new instance of the class, and then copying all properties of
that instance to my actual instance like this:

XmlSerializer deserializer = new XmlSerializer(t ypeof(MyClass)) ;
StringReader strReader = new StringReader(xm lData);
XmlReader xmlReader = new XmlTextReader(s trReader);
MyClass myClass = new MyClass();

myClass = (MyClass)deseri alizer.Deserial ize(xmlReader);
moduleID = myClass.moduleI D;
boID = myClass.boID;
parentModuleID = myClass.parentM oduleID;
parentBoID = myClass.parentB oID;

AND NOW THE QUESTION...

Is there a way I could iterate through the public properties of the class to
automatically copy all properties of that instance to my actual instance?
Maybe using Reflection?

I want to make it so that if I add more public properties they will
automatically be deserialized so that I don't have to specify for them to be
copied to my actual instance.

Thanks,

John
Nov 17 '05 #1
0 1455

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

Similar topics

0
1697
by: alex | last post by:
I am trying to use XmlSerializer to serialize the XML string into an object. I used the XML schema definition to generate the code for the corresponding class (using xsd.exe). The top level class has references to objects of other classes that are defined in the same schema. I am trying to deserialize the XML string into an object of the...
0
1968
by: Casey | last post by:
So I'm using XmlSerializer to serialize out a wrapper object that contains an arbitrary number of other objects. The class definitions listed below are made to be very generic. Some of the objects contain other objects so when I declare my XmlSerializer I need to send along an array of types to be included in the serialization. That works...
2
2169
by: Andrew G. J. Fung | last post by:
Can anyone please explain to me why the code below throws an exception? It seems to occur when deserializing an instance of a subclass, whose base class holds a struct, where said struct holds an instance to a reference type (but not System.String). The commented out code are lines that when enabled (and any conflicting lines disabled) stop...
7
16051
by: farseer | last post by:
Hi, i am getting an error when trying to deserialize and xml. the error is shown below. i get this error regardless if i try to desearialize in C# or VB. the XML itself does infact contain a top level "PDocument" note which contains an xmlns namespace and some other attriute. is the error belowing telling it doesn't like that namespace...
2
3748
by: Phillip Galey | last post by:
I have an object called Place which contains only string properties and has the <Serializable()> flag before the class name declaration. I also have a collection object called Places, which is implemented using Inherits System.Collections.Specialized.NameObjectCollectionBase and also has the <Serializable()> flag before the class name...
9
2097
by: Dennis | last post by:
When a class (myownclass) inheirits another class, how can I get an object reference to the underlyng MyBase class instance from within myownclass. The base class has a method that I want to utilize in myownclass but the method is private and can't be inheirited. I need to utilize that base class method. -- Dennis in Houston
7
2375
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...
7
1688
by: joproulx | last post by:
Hi, I was wondering if there was a way with Reflection to find dynamically if an object was referencing indirectly another object. A simple example would be: Object1 | --Object2 |
0
7924
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. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
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...
0
7968
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...
1
5512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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...
0
3653
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
937
bsmnconsultancy
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...

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.