473,395 Members | 1,885 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,395 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 1297

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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...
0
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...
0
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,...

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.