DHN
DHN

Reputation: 4865

XmlSerializer: XSD generated classes serialization issues

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.

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.

  1. Add and implement IXmlSerializable interface to the generated classes of the affected complexTypes.
    Unfortunately, this is not an option since the initialization of the XmlSerializer 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.
  2. 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.
  3. Modify the string content of the nodes via XPathNavigator after serialization.
    Not the best approach but it would do the job.
  4. 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 a partial 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.

Upvotes: 0

Views: 124

Answers (1)

DHN
DHN

Reputation: 4865

Due to the tremendous feedback, I've decided to go with option 3.

Reason is simple. We can still use the standard class generation with XSD.exe and keep all benefits of XmlSerializer class.

For more information please refer to this article in the MSDN. The flow is pretty straight forward.

  1. Select nodes with the given path
  2. Read value untyped (string) and parse expected typed object
  3. Write untyped value from typed object by using the desired format

We have tested it already and it's working like a charm.

Upvotes: 0

Related Questions