views:

135

answers:

3

I am using classes that were generated from an XML schema using the xsd.exe tool. It currently generates a huge (32k line) .cs file. I then serialize and deserialize parts of the of model using XMLSerializer. I need to override properties in these classes, so I have partial classes in separate files that override some of these generated methods. However, this means going in and marking hundreds of methods as virtual every time the schema changes. Is there a way to get the xsd.exe tool to mark methods as virtual when they are generated?

+2  A: 

The output from XSD.exe is not very customizable.

I had a similar problem a while ago and created a simple console application that modified the generated output from XSD.exe.

HakonB
Good point - it's quick'n'dirty, but also a bit "take it or leave it" in its approach :-)
marc_s
True, but XSD.exe is IMO very much a black box tool. A tool that in my case did 99% of what was needed and I didn't feel a bit dirty by hacking to get the last 1% :)
HakonB
In the past I've done this via batch file. Not as fast as writing a C# app, but the former was IMO overkill for twiddling one or two things, especially when I already had a batchfile in place to provide commandline parameters and move the output into my source folder.
Dan Neely
A: 

It seems odd to me that you need to modify the serialization code. If you need to override properties, can't you simply create new properties that kind of wrap the existing ones yet add new behaviour? Or did I miss the point.

Serge - appTranslator
Sure, and in some cases I have. Given the size of the schema and the legacy codebase it's going to be a while before I wrap it all.
MattMcKnight
A: 

If you have to do this on an ongoing basis, you should look into code generation of some kind. Build the XSD-generated types, then write an application to load those types, and use Reflection to generate types that are identical except that all the properties are virtual. You will then be able to derive from the new types and override the properties as necessary.

This can become part of your build process.

John Saunders