문제

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:

<ItemGroup>
  <Preprocess Include="Xml\MySourceXmlFile1" />
  <Preprocess Include="Xml\MySourceXmlFile2.xml" />
</ItemGroup>

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:

<Target Name="BeforeBuild"
  Inputs="@(Preprocess)"
  Outputs="@(Preprocess->'$(ProjectDir)%(Filename).cs')">
  <Exec Command="xsltproc -o %22$(ProjectDir)%(Filename).cs%22 %22MyTransform.xsl%22 @(Preprocess)" />
</Target>

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top