7

I need a way to automatically regenerate *.cs files during build, based on *.xsd files, preferably without involving any custom add-ins. This needs to run on the CI build as well.

I'm not sure if I'm missing something obvious, or is this really tricky as it seems to me?

Marcin Seredynski
  • 7,057
  • 3
  • 22
  • 29

1 Answers1

8

I use this script:

@echo off
cd %1
call :treeProcess %2 "XSDs"
cd ..
goto :eof

:treeProcess
rem From http://stackoverflow.com/a/8398621/298754
echo Processing %2
for %%f in (*.xsd) do call :buildXSD %%f %1 %2 %%~nf
for /D %%d in (*) do (
    cd %%d
    call :treeProcess %1 %2.%%d
    cd ..
)
exit /b

:buildXSD
%2 %1 /c /n:%3.%4%

with a prebuild event of

call "$(ProjectDir)"XSDBuilder.bat "$(ProjectDir)"\XSDs "$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.0A\@InstallationFolder)bin\xsd.exe"

This will recursively parse every .xsd file in a folder in the project root called XSDs, and will assign a namespace based on the folder structure.

Bobson
  • 13,498
  • 5
  • 55
  • 80
  • Thank you, this has worked for me! Sometimes the most obvious answer is the hardest to find. – Marcin Seredynski Feb 15 '13 at 16:37
  • @MarcinSeredynski - Definitely true. I'm glad it helps! – Bobson Feb 15 '13 at 16:40
  • 1
    To setup your own namespace you can replace "XSDs" string literal on third line either with your namespace or with %3, to be able to pass namespace in third parameter to this batch file – Woodman Nov 01 '13 at 14:49
  • 1
    for x64 compatibility use this string instead of second parameter and check version of SDK installed on your machine "$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\Windows\v7.1', 'InstallationFolder', null, RegistryView.Registry64, RegistryView.Registry32))bin\xsd.exe" – Woodman Nov 01 '13 at 14:51
  • If you are watching for changes in the XML from which the XSD was generated, be aware that you must generate the XSD the first time around, then look for XSDs thenceforth. Otherwise, you'll generate CS files for a whole bunch of XML documents that don't warrant it. – David A. Gray Dec 28 '16 at 00:57