473,379 Members | 974 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,379 software developers and data experts.

XML Serializing a simple Structure

Hi all

I have made two simple methods to serialize/deserialize a simple data
structure to my database using SoapFormatting.

This actually works just great, when the structures stays the same. BUT if I
change the structure, say adds a field, I get an error when trying to
deserialize the allready serialized object from the database saying that the
number of fields of the structure does not match the number of fields in the
one I'm trying to deserialize.
Thids is quite a problem since I'm not able to make any future changes to my
Does anyone know how to get about this problem ??
Thanks in Advance

Public Shared Function StreamToString(ByVal st As System.IO.Stream) As
st.Position = 0
If st.CanRead And st.CanSeek Then
Dim by(Convert.ToInt32(st.Length - 1)) As Byte
st.Read(by, 0, by.Length)
st.Position = 0
Return System.Text.Encoding.UTF8.GetString(by)
End If
End Function

Public Shared Function StringToStream(ByVal s As String) As System.IO.Stream
Dim by(s.Length - 1) As Byte
by = System.Text.Encoding.UTF8.GetBytes(s)
Dim st As System.IO.Stream = New System.IO.MemoryStream(s.Length)
st.Write(by, 0, by.Length)
st.Position = 0
Return st
End Function

Public Shared Function ObjToXml(ByVal obj As Object) As String
Dim Stream As System.IO.Stream = New System.IO.MemoryStream()
Dim SoapFormatter As System.Runtime.Serialization.IFormatter = New
System.Runtime.Serialization.Formatters.Soap.SoapF ormatter()
Dim SoapString As String

SoapFormatter.Serialize(Stream, obj)

SoapString = StreamToString(Stream)
Stream = Nothing

Return SoapString
End Function

Public Shared Function XmlToObj(ByVal Xml As String) As Object
Dim SoapFormatter As System.Runtime.Serialization.IFormatter = New
System.Runtime.Serialization.Formatters.Soap.SoapF ormatter()
Dim Stream As System.IO.Stream = StringToStream(Xml)
Dim obj As Object

obj = SoapFormatter.Deserialize(Stream)
Stream = Nothing
Return obj
End Function´

Nov 20 '05 #1
1 2253
Hi Farouche,

This is an age old problem rearing its head in the arena of XML - What do
I do when I have existing data and I've changed the data format?

The simple answer is that you have to convert it. You'll need to know when
you are receiving an old format object and call a converter that inserts the
added fields and removes any deleted field, converts any values that have
changed type, etc, etc.

One way to do this is to place your XML into a System.Xml.XmlDataDocument
and manipulate it using that. You can do the transformation using code or
using XSLT if you know it. Then take the XML out again and pass it to the

Another way is to have two deserialisers - one that works for the old kind
of object and one for the new. [Q. Why does your code accept Object - why not
the actual type?]. Every time that you change your format you'll have to add
another deserialiser to your collection. The 'old' deserialiser would populate
an 'old' object which you would then transfer to an instance of a 'new'

If you have no way of telling whether you have an old or new format
serialised object, then you must use the new serialiser, wait for the
Exception, catch it and then call the old serialiser. If you have several
versions then you must cascade them. Ughh!

To guard against having to jump through all these hoops, it is a good idea
to start versioning them so that you can detect which deserialiser you'll

Another possibility is to add in all the fields that you can anticipate at
this time. This will cut down the amount of versioning but there are bound to
be unanticipated fields.

Another possibility, still nothing elegant, is to have an 'overflow'
object in which you add new fields. This could be simply an 'oMoreFields As
Object' field. You could utilise inheritance so that succesive versions are
still contained within the same clas hierarchy.

Another possibility, and one that leaves your program unblemished, is to
write a utility program which does the conversion for you. It would have to be
rewritten (well, edited) for each version change. It would take all the
existing serialised data and apply the required adds/changes/deletes.

Choices, choices. Decisions, decisions :-(

And there will be more from others too, I expect. :-)

Nov 20 '05 #2

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

Similar topics

by: rgerla | last post by:
I'm trying to serialize a compount structure set for a web service, the sturctures are something like this: Public Structure UserPhone Public phonenumber As String Public phonetype As Char End...
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b)...
by: Alfonso Morra | last post by:
Hi, I am writing a messaging library which will allow me to send a generic message structure with custom "payloads". In many cases, a message must store a non-linear data structure (i.e....
by: ~~~ .NET Ed ~~~ | last post by:
Hello, I am having a problem trying to serialize this simple structure, I use it in a collection class that derives from CollectionBase but I noticed that at least for now the serialization...
by: olsonchris | last post by:
Hello all, I have what appears to be a simple question but after quite a bit of research I can't seem to find an answer. Basically, I am serializing a class into an XML document. This is...
by: Dennis | last post by:
Does anyone know why this code gives an error: <serializeable> public structure TestSerialize anumber as integer astring as string end strucuture Dim t As TestSerialize Dim formatter As New...
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hi, I know this will sound like a lot of hand waving, and I'll be glad to supply some sample code if necessary, but I thought something obvious might jump out at someone without doing so. ...
by: Cagdas Ozgenc | last post by:
Greetings, When directly serializing C++ structures to a file with the standard library functions giving the address of the data and length of structure using the sizeof operator, do I risk...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.