Upgrading XML Schema programmatically
I'm in the process of having to re-write a project due to 开发者_如何转开发the platform vendor needing the storage mechanism be in an XElement object instead of storing in data tables that I create in their database. It's becoming painful, and I need some insight.
I can morph my object graph to/from Xml using the XmlSerializer and read/write to/from the XElement object that I'm given, but I'm not sure how to upgrade the Xml Data when I need to change the object graph.
Has anyone confronted this, and what articles have proven the best to learn how to build such an update methodology. Thanks.
I've been in this frustrating situation before... I think this can't be done using the XmlSerializer. The XmlSerializer compiles a serialisation routine that is custom-built around your current type hierarchy. If you change that too much, the XML will become incompatible with the new class structure.
I don't think any XML Schemas get generated, the only 'schema' is the compiled ad-hoc XML serializer code.
You could do what you want by 1) creating new classes that reflect your changes, 2) deserialising the XML into the old classes, 3) applying the morph between old and new objects, 4) serialising the new objects, 5) changing names of classes to the old ones, 6) replacing the temp names in the XML,
but it's extremely painful (though it may be ok as a one-off).
If you need to cope with frequent changes (or if you use serialisation-unfriendly compilers like the current F# one), the best idea here is to write your own XmlSerializer (possibly using Linq2XML) that can cope with code changes.
精彩评论