Advice on XML vs. binary serialization?

D

Dave Veeneman

I'm working on a project where I have to persist data to a file, rather than
to a database. Basically, I need to save the state of several classes, each
of which will have a couple of dozen instances. I have set it up to use
binary serialization to a single file, which works well. However, I have
learned that changes to my classes break the old serialized files.

While my object model is pretty stable, I am concerned that using binary
serialization will limit the ability to make changes in the future. So, I'm
thinking about adding XML serialization, either as an export option, or as a
replacement for the binary serialization I am using. That way, I can provide
a mechanism for reading prior version data files, even if a binary
serialization would be broken by the changes.

What would you suggest I do? What do you see as the benefits and drawbacks
of each approach? If I go XML serialization, should it be an export option,
or a replacement for binary serialization? Thanks for your help!

Dave Veeneman
Foresight Systems, Inc.
 
W

William Stacey [MVP]

Not expert here. But bin serial would bind your input to a clr type and
version (I think.) Using xml, you could ignore unknown elements and still
process known elements. Your config file will also be in xml, so at least
you give users some ability to change things if needed and diag is a bit
easier because of readability, etc. If you need to do this many times in a
row, xml may not be best perf option as you know.
 
M

Mickey Williams

You can handle versioning by implementing ISerializable and embedding a
schema version number in the serialized data. When you make changes to the
class, increment the version number. When you deserialize, use the version
number as a guide when reconstituting the instance. Alternatively, you can
sreate a serialization surrogate - it's more flexible but a bit more effort.
Search for ISerializationSurrogate in the online help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top