1

I have a complex graph of XML-serializable classes that I'm able to (de)serialize to hard-disk just fine. But how do I handle massive changes to the graph schema structure? Is there some mechanism to handle XML schema upgrades? Some classes that would allow me to migrate old data to the new format?

Sure I could just use XmlReader/XmlWriter, go through every node and attribute and write several thousand lines of code to convert data to the new format, but maybe there is a better way?


I have found Object graph serialization in .NET and code version upgrades, but I don't think the linked articles apply when there are major changes in the model.

Community
  • 1
  • 1
Paya
  • 5,124
  • 4
  • 45
  • 71

2 Answers2

2

Instead of writing several thousand lines of code to convert files using XmlReader / XmlWriter, you could use XSLT. We are still talking hundreds of lines of code, and perhaps slower execution speeds, but if you are good at XSLT you could get it done much faster.

The other approach would be to build a C# program that links both the old class and the new class (of course you'd need to rename the old class to avoid naming collision). The program would load OldMyClass from disk, construct NewMyClass from the values of its attributes, and serialize NewMyClass to disk. Essentially, this approach moves the task of conversion into the C# territory, which may be a lot more familiar to you.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • +1 Thanks, XSLT looks very promising. But according to http://stackoverflow.com/questions/1525299/xpath-and-xslt-2-0-for-net it seems XSLT 2.0 is not yet implemented in .NET. And since XSLT 1.0 is almost 13 years old, I wonder if it's powerful enough to handle complex changes. – Paya Feb 10 '12 at 14:55
  • @Paja Since this is a one-time deal, you do not need a .NET implementation: use any XSLT engine that you can run from the command line. Feed the engine your "script" and your data, and save the output that it produces. I used [Saxon](http://www.perfectxml.com/SoftDetails.asp?id=193), but it was long 11 years ago; newer engines have been written since then, you may want to do some research before picking one for the task. Admittedly, writing XSLT has a relatively steep learning curve, but once you get over the initial hump, you'll be able to write lots of complex conversions very quickly. – Sergey Kalinichenko Feb 10 '12 at 15:03
  • I'm writing a program for end-users, so the migration algorithm needs to be distributed with it. Because of that, an implementation directly in .NET framework would be awesome, as I don't like bloating my setup with additional huge libraries. – Paya Feb 10 '12 at 15:10
  • @Paja Although XSLT 1.0 spec is relatively old, I remember it being very, very powerful. Many 2.0 features may not be of much interest to you (e.g. support for multiple output files) and others may be a convenience, rather than necessity (e.g. user-defined functions). So I would start with 1.0, and upgrade to 2.0 only if you absolutely need to. Altova's Community Edition gives you a royalty-free upgrade path in case you cannot complete the task entirely in 1.0. – Sergey Kalinichenko Feb 10 '12 at 15:27
0

In this case, i keep my changes in my object and recreate my xml through the XmlSerializer: http://support.microsoft.com/kb/815813

With this i load and save new xml schema based in my object.

Vinicius Ottoni
  • 4,631
  • 9
  • 42
  • 64