I have a pre-build event on a C# project which runs xsltproc to transform some XML into C# source files. The generated source then gets built in the normal way. This means that the project always gets built regardless of whether the XML has changed.
Is there a way of only generating the C# classes if the XML has changed? Is a pre-build event the wrong approach? Would I be better off with a custom tool of some kind to turn the XML into C#?
I’m using Visual Studio 2010. The XML doesn’t contain serialized objects.
Any help much appreciated.
In the end I added a new ItemGroup to my csproj file with references to each XML file. I gave each item an element name of Preprocess:
Later in the project file I overrode the BeforeBuild target, so that it transforms every Preprocess item into a C# source file with the same name as the XML file:
Note the arguments to xsltproc have to be escaped with %22.
Now the C# source file is only built if the XML has changed. I got the approach from this forum post.