Consider the following scenario. We have a XSD file defining a technical document interchange format (one root type composed of several complexTypes
).
Additionally, there is a specification defining defining each and every field with value range and format.
Obviously, both documents have been created by different departments because the formats are defined differently. E.g.
- XSD: Time is represented by xml type time (
XmlSerializer
format: HH:mm:ss.fff) - Spec doc: The following format must be used for time HH:mm.
Technical wise, things are easy. We used XSD.exe to generate the classes. But now the output file is looking different after using XmlSerializer
. There is no chance to negotiate the format, since it was created due to a regulation affecting a whole market and therefore a lot of counterparties.
As per the moment we have to assume, that the formats of the specification document need to be respected. So I was looking for options to ensure this.
- Add and implement
IXmlSerializable
interface to the generated classes of the affectedcomplexTypes
.
Unfortunately, this is not an option since the initialization of theXmlSerializer
is throwing an exception: "There was an error reflecting type."
Or is there any way to avoid this? Implementing the interface for the root type is not an option, since it's very large and complex. - Find a serialization library, where some kind of type format mapping can be provided, so that a custom format in fields can be implemented.
Right now, I didn't find anything like this. But perhaps someone can give me hint. - Modify the string content of the nodes via
XPathNavigator
after serialization.
Not the best approach but it would do the job. - Modify the generated classes directly in the generation output and add some proxy properties.
Actually not the best idea, since a regeneration (due to a new version of the XSD file) is overwriting all modifications. Unfortunately, properties cannot be redefined in apartial class
, right?
I've ordered the list by the order of preference.
So did I miss an option? Which way would you go?
I know it's not the typical How do I use class xyz question, but still I'm hoping, that you can give me hint, how such kind of scenarios are handled usually.