473,320 Members | 1,732 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,320 software developers and data experts.

.NET Serialization Quest

Hi All,

I have a question concerning serialization in .NET.

If I serialize an object to a database and then I change the object in code
(i.e. add a property, rename a property, delete a property) will my object
deserialize correctly?

Will it throw an exception - or will it deserialize whatever it can?

If serialization is going to throw an error, is there a way to do a "best
effort" deserialization or handle changed objects?

Thanks!
Nov 14 '06 #1
5 953

Spam Catcher wrote:
Hi All,

I have a question concerning serialization in .NET.

If I serialize an object to a database and then I change the object in code
(i.e. add a property, rename a property, delete a property) will my object
deserialize correctly?

Will it throw an exception - or will it deserialize whatever it can?

If serialization is going to throw an error, is there a way to do a "best
effort" deserialization or handle changed objects?

Thanks!
I have another question: how are you serializing your objects to the
database? I just started work on a similar problem and I haven't even
gotten the objects to the database yet. If I can get that far, I'll let
you know how well they come back out of the database.

Nov 14 '06 #2

Spam Catcher wrote:
Hi All,

I have a question concerning serialization in .NET.

If I serialize an object to a database and then I change the object in code
(i.e. add a property, rename a property, delete a property) will my object
deserialize correctly?

Will it throw an exception - or will it deserialize whatever it can?

If serialization is going to throw an error, is there a way to do a "best
effort" deserialization or handle changed objects?

Thanks!
I have another question: how are you serializing your objects to the
database? I just started work on a similar problem and I haven't even
gotten the objects to the database yet. If I can get that far, I'll let
you know how well they come back out of the database.

Nov 14 '06 #3
lo*********@gmail.com wrote in news:1163520931.928255.279730
@i42g2000cwa.googlegroups.com:
I have another question: how are you serializing your objects to the
database? I just started work on a similar problem and I haven't even
gotten the objects to the database yet. If I can get that far, I'll
let
you know how well they come back out of the database.
Here's my code:

Public Shared Function Serialize(ByVal instance As Object) As String
Dim _Serializer As New SoapFormatter

Using _MS As New MemoryStream
Try
_Serializer.Serialize(_MS, instance)

Return System.Text.Encoding.ASCII.GetString(_MS.ToArray)
Catch ex As Exception
Return ""
End Try
End Using

End Function

Public Shared Function Deserialize(ByVal XML As String) As Object
Dim _Serializer As New SoapFormatter

Using _MS As New MemoryStream
(System.Text.Encoding.ASCII.GetBytes(XML))
Try
Return _Serializer.Deserialize(_MS)
Catch ex As Exception
Return Nothing
End Try
End Using
End Function
Nov 14 '06 #4
That depends on how you do it. I'm assuming you're serializing to XML
using an IXmlSerializable implementation. In that case, you're pretty
much in charge of the serialization and therefore the behavior under
fringe cases.

If this is the case, the story goes something like this:

1) You have your object. You create an XmlSerializer that'll spit out
either a file or a stream containing your XML upon request. You can
either tie the stream to blob access or turn the stream in to a string
and write it parametrically.

2) When you get your object back, you get the same, either a string or
a stream. You create an XmlSerializer and pass it your data as a
stream.

3) It'll call your null constructor then the implemented ReadXml()
method. Whatever you've chosen to write in the method will work as
written.

The implication of this is that you can make your ReadXml() method as
robust to change as you want. If the version 1.0 object has members A
and B and version 2.0 adds C, everything *can* work out if ReadXml() is
overridden correctly and C is nullable or defaultable.

As far as properties and such, you're on your own. Serialization
couldn't care less about anything outside the IXmlSerializable
implementation.
Stephan

Spam Catcher wrote:
Hi All,

I have a question concerning serialization in .NET.

If I serialize an object to a database and then I change the object in code
(i.e. add a property, rename a property, delete a property) will my object
deserialize correctly?

Will it throw an exception - or will it deserialize whatever it can?

If serialization is going to throw an error, is there a way to do a "best
effort" deserialization or handle changed objects?

Thanks!
Nov 14 '06 #5
First, you need to separate xml-serialization and
binary/SOAP-serialization; they work differently.
When adding fields/properties:
For binary, you can look at [OptionalField], or provide your own "best
efforst" implementation via ISerializable without too much trouble.

For xml, you can look at [DefaultValue], which has the effect of making
a property optional. You could also implement IXmlSerializable, but
IIRC it is a pig (providing the schema, mainly).

When deleting: you tell me. A simple test app should take about 3
minutes to write...

Marc

Nov 14 '06 #6

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

Similar topics

1
by: Trent | last post by:
Hello, I was wondering if anyone here could help with a Quest Central issue. I have two different hosts listed in Quest Central. Each host has a database on it, but the databases have the...
3
by: Robert Stearns | last post by:
Apparently due to an intellectual property suit by CA, Quest Central for DB2 has gone away. They can/will not sell any license extensions. In a project I am doing, we make extensive use of quest...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
5
by: Spam Catcher | last post by:
Hi All, I have a question concerning serialization in .NET. If I serialize an object to a database and then I change the object in code (i.e. add a property, rename a property, delete a...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
5
by: Nikola Skoric | last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK until I tried to desirialize a object. There the program died abruptly dumping this: System.ArgumentOutOfRangeException:...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
4
by: bj7lewis | last post by:
I am currently C++/Win32/MFC & C# programmer using VS.NET 2002 and jumped into VS.NET 2003 but that was a waste so I skipped VS.NET 2005 cause buying VS.NET 2003. Now after checking out VS.NET 2008...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.