views:

424

answers:

2

I'm looking at some work updating classes generated from xsds. I think this would be a great use of T4 templates, but I don't see any examples of people doing this. Essentially, I'd like to do what Linq to XSD has done, but without including alpha build targets in a production solution.

Any relevant links out there?


Now a wiki. Store your relevant answers here.

+1  A: 

I found XSD to be very difficult to use as a model for code generation. It is too flexible and complex to be practical for my needs. The strongly-typed DataSet designer/generator is a great example of an attempt to use XSD as the code generation model. It works OK most of the time, but does not support all possible schemas you could throw at it. On the other hand, XSD cannot represent information required to generate table adapters directly, so DataSet-specific extensions had to be created.

If you decide to go ahead and use XSD as the model, you will probably want to pick a simple subset, sufficient for what you need to generate. One approach could be to load XSD schema into a DataSet object and use this DataSet object as the model. DataSet metadata is a lot simpler to access than general-purpose XSD. This would also allow you to use the existing DataSet designer in Visual Studio to edit your models.

Oleg Sych
The code generation is for classes that will hold deserialized xml. Either I build out my own models and parse xml into them, or just deserialize objects from xml directly. Looking for a more lightweight solution than datasets... one that allows me to write partial classes for the deserialized data.
Will
+2  A: 

I'm currently using xsd2code.net, find it here: http://xsd2code.codeplex.com/. Lots of benefits:

  • open source, so you can tune/hack it

  • replace arrays with lists or even your own custom collection type

  • run from commandline, or run as a visual studio addin (right click xsd, tell to generate)

I also like that you can run it on a single xsd file and it wont blow up if referenced xsd's cannot be found (like how xsd.exe does). So it's easy to run it on individual files, in case you just want to do a quick update on one file.

However, T4 should be an option as well, I just googled T4 XSD and found a few solutions talking about this.

boomhauer
I think this is the best solution at this point. A better soulution is probably to avoid the situation to begin with.
Will
To update on this: Im currently reviewing this project- http://oxmlibrary.codeplex.com/ - It seems a bit hackish but more "hackable" if that makes sense.
boomhauer
Will, I'm back doing this exact thing again. Avoidance may be best, but in the case where an industry standard has been defined in xsd, generating classes from the schema is a very valid operation. Though not much fun.
boomhauer