473,659 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialization, XSD to class, and deep copying?

I have an XSD which I have generated a class from using the xsd.exe
tool. My XSD contains complex types within it, so that the generated
class has member variables which are of types that are also defined in
the XSD.

I deserialize XML data into instances of that class, and serialize back
to XML. So far so good.

However, in my code I have the concept of wanting to create new copies
of these deserialized objects and modifying some of the members of the
new copies. This obviously presents a problem because I don't want the
member variables referencing the same data.

In other words I *think* I want to do a deep copy, but everything I've
read on "C# copy constructors and ICloneable" seems to point to this
area being a route to no-where. Besides which, my generated classes
don't support ICloneable and I can't keep writing my own copy
constructors each time I generate the class.

So... is there a design pattern for this model that can help me
overcome these problems? It seems that I could make use of
serialization to create new copies, which is handy because my generated
classes, by nature, support serialization.

Any advice or references on "the right thing to do" would be much
appreciated.

Boz

Mar 2 '06 #1
4 3032
What makes you think that implementing "ICloneable " is a road to nowhere?
You can certainly add methods to your class. When it is serialized, the
method will not be there, but when it is deserialized, the method *will* be
there.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
<jj********@yah oo.com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.com.. .
I have an XSD which I have generated a class from using the xsd.exe
tool. My XSD contains complex types within it, so that the generated
class has member variables which are of types that are also defined in
the XSD.

I deserialize XML data into instances of that class, and serialize back
to XML. So far so good.

However, in my code I have the concept of wanting to create new copies
of these deserialized objects and modifying some of the members of the
new copies. This obviously presents a problem because I don't want the
member variables referencing the same data.

In other words I *think* I want to do a deep copy, but everything I've
read on "C# copy constructors and ICloneable" seems to point to this
area being a route to no-where. Besides which, my generated classes
don't support ICloneable and I can't keep writing my own copy
constructors each time I generate the class.

So... is there a design pattern for this model that can help me
overcome these problems? It seems that I could make use of
serialization to create new copies, which is handy because my generated
classes, by nature, support serialization.

Any advice or references on "the right thing to do" would be much
appreciated.

Boz

Mar 2 '06 #2
I said road to nowhere based on what I found on the net regarding deep
copying (copy constructor) for ICloneable and also item 28 in Effective
C# "Avoid ICloneable".

Further discussions in house have revealed a quite neat solution:
- derive a class from the XSD generated class
- implement ICloneable on the derived class
- our Clone() method uses serialization & deserialization create new
copy

Does this sound a reasonable solution? The only downsides I can see
might be in terms of performance, in that we're using serialization
twice and there's boxing/unboxing going on because Clone returns
System.Object.

Boz

PS Referenced posting regarding deep copy and ICloneable being road to
nowhere, from another MVP like yourself:
http://groups.google.co.uk/group/mic...e&rnum=5&hl=en

Mar 2 '06 #3
Hi Boz,
Further discussions in house have revealed a quite neat solution:
- derive a class from the XSD generated class
- implement ICloneable on the derived class
- our Clone() method uses serialization & deserialization create new
copy

Does this sound a reasonable solution? The only downsides I can see
I couldn't tell you whether it's a reasonable solution in this specific
case. After all, you're still implementing ICloneable, regardless of whether
you're implementing it in the base class or the derived class.

And ICloneable may be something which can be abused, but it is also useful.
Otherwise it wouldn't be there.
PS Referenced posting regarding deep copy and ICloneable being road to
nowhere, from another MVP like yourself:
Well, Boz, speaking as an MVP (and a human being), I would have to say that
one should not take anything told to one by anyone, MVP or not, as Gospel.
When I post a suggestion, I expect the person I'm responding to to do some
research and ensure that I'm on the right track. I have been wrong a time or
two, and who hasn't? What I hope for is that what I tell someone will be
enough to stimulate or move that person in the right direction.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.
<jj********@yah oo.com> wrote in message
news:11******** **************@ t39g2000cwt.goo glegroups.com.. .I said road to nowhere based on what I found on the net regarding deep
copying (copy constructor) for ICloneable and also item 28 in Effective
C# "Avoid ICloneable".

Further discussions in house have revealed a quite neat solution:
- derive a class from the XSD generated class
- implement ICloneable on the derived class
- our Clone() method uses serialization & deserialization create new
copy

Does this sound a reasonable solution? The only downsides I can see
might be in terms of performance, in that we're using serialization
twice and there's boxing/unboxing going on because Clone returns
System.Object.

Boz

PS Referenced posting regarding deep copy and ICloneable being road to
nowhere, from another MVP like yourself:
http://groups.google.co.uk/group/mic...e&rnum=5&hl=en

Mar 2 '06 #4
Kevin,

I was hoping to talk through ideas to help me reach a decision on how
to achieve a "deep copy", but perhaps discussing that here is going to
be a road to nowhere :-)

Anyway, I've been scouring the net on this subject and its very clear
that the subject is not clear cut. Check out this debate on obseleting
ICloneable. It expresses the issues involved and the differing
opinions.

http://www.dotnet247.com/247referenc...03/125427.aspx

Boz

Mar 3 '06 #5

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

Similar topics

14
4490
by: Arthur | last post by:
A bit inspired by the decorator discussions, I'm trying to tackle something I had been avoiding. Essentially I am trying to create a non-destructive tranformation of an instance of a class - is one way of putting it. The way I am currently conceptualizing a solution, what I need is a method of the class that returns a new instance of the class. I'm sure this is not new territory.
1
6037
by: Jesper Denmark | last post by:
Hi, Is deep serialization possible using XML. Know any good tutorials?. By deep serialization I mean the ability that an object being serialized automatically serialize its members. E.g Class A {
2
5553
by: Jesper Denmark | last post by:
Hi, I've had a lot of trouble finding a good tutorial showing deep serialization in XML in C#. All samples I've found shows merely serialization of a single class. If I want to serialize members of a class like the 'data' member of the Person class when I serialize the person object - which attributes do I need to use? public class Person
1
2630
by: Opa | last post by:
Hi I'm having problem serialization an object instance which contains a public property on the object type My object hierarchy is many levels deep, so for simplicty I created to following which produces the same error Let's say there is a class called ParkingSpot with a public member Vehicle having an object type of object For simplicity, let's say that Vehicle could be anything, hence I create a new object of Car typ and assign it to...
1
2862
by: Opa | last post by:
Hi I'm having problem serialization an object instance which contains a public property on the object type My object hierarchy is many levels deep, so for simplicty I created to following which produces the same error Let's say there is a class called ParkingSpot with a public member Vehicle having an object type of object For simplicity, let's say that Vehicle could be anything, hence I create a new object of Car typ and assign it to...
2
1473
by: fperfect13 | last post by:
The problem is like this: I have a class C1 which has a private field "private Graph graph;" accesed by a property(C1.Graph). The class Graph is serializable but when i pass to a method that serializes a Graph object C1.Graph it gives an error saying that C1 isn't serializable. The project the contains the C1 class has a reference to the project with Graph class.(the proj with Graph class has no references to C1's class proj). The...
1
2335
by: oDDskOOL | last post by:
I realized today that the Hashtable.Clone only produces a shallow copy... that makes me go mad that M$ doesn't even provide a deep copy ctor for the Hashtable class ! mighty tech ducks might reply "oh but what if the types in hastable don't have copy ctors defined? that could lead to dangerous situations". well, but was it a significant effort to provide a copy ctor / deep clone method to this class for objects that provide the...
15
2396
by: Jacques | last post by:
Hi I am an dotNet newby, so pardon my ignorance. I am looking for a method of saving/copying a managed class to a stream/file WITHOUT saving the object's state, eg. if I have a ref class with two int32's as its data members, the binary file of that class must have a size of 8 bytes (i.e. only contains class data members, not methods etc.). Is serialization the answer to the above problem? If I understand correctly, the reason that...
5
3994
by: Henry Jones | last post by:
I read a bit on Serialization and came up with the following definition: The System.Runtime.Serialization namespace contains classes that can be used for serializing and deserializing objects. Serialization is the process of converting an object or a graph of objects into a linear sequence of bytes for either storage or transmission to another location. Deserialization is the process of taking in stored information and recreating objects...
0
8332
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
8851
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...
0
7356
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...
1
6179
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
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
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
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
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.