473,396 Members | 1,929 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,396 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
data.
Does anyone know how to get about this problem ??
Thanks in Advance
Farouche

Public Shared Function StreamToString(ByVal st As System.IO.Stream) As
String
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.Close()
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.Close()
Stream = Nothing
Return obj
End Function´


Nov 20 '05 #1
1 2255
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
deserialiser.

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'
object.

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
need.

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. :-)

Regards,
Fergus
Nov 20 '05 #2

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

Similar topics

0
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...
10
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)...
5
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....
3
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...
0
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...
2
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...
6
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...
2
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. ...
12
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
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:
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.