I use Xsd2Code a lot, but my approach is to add a pre-build event that calls the Xsd2Code command line and regenerates xml for each assembly.
My pre-build event is as follows:
$(ProjectDir)BuildTools\Xsd2Code.exe $(ProjectDir)Api\Schemas\MySchema.xsd MyProject.Api.Schemas $(ProjectDir)Api\Schemas\MySchema.cs /platform Net40 /collection Array /sc+ /ap+ /if- /xa+
In your case, you can perform this pre-build step only on the main xsd (which I guess xsd: Imports other schemas), or you can run the command for each of your schema files separately.
The advantage of this is that if I change the XSD schema, I get very useful compile-time errors :)
Hope you get some ideas!
EDIT
I spent some time thinking about the problem you identified regarding the build time, and modified the pre-build script as follows:
$(ProjectDir)BuildTools\Xsd2Code.exe $(ProjectDir)Api\Schemas\MySchema.xsd MyProject.Api.Schemas $(ProjectDir)Api\Schemas\MySchema.cs.temp /platform Net40 /collection Array /sc+ /ap+ /if- /xa+ fc $(ProjectDir)Api\Schemas\MySchema.cs $(ProjectDir)Api\Schemas\MySchema.cs.temp if errorlevel 1 copy $(ProjectDir)Api\Schemas\MySchema.cs.temp $(ProjectDir)Api\Schemas\MySchema.cs /Y del $(ProjectDir)Api\Schemas\MySchema.cs.temp
So, Xsd2Code now generates the source code in a temporary file that only overwrites the existing .cs file if it is different. This should mean that if .xsd has not changed at all, then .cs will not be generated :)
You still take a hit at running xsd2code, but you don't take a hit at msbuild, rebuilding a whole chain of projects if the generated source was the same.
MattDavey
source share