Use XSD Build task in c# project

烈酒焚心 提交于 2019-12-05 16:56:48

Forget the C++ bit, it's a distraction from the msdn page title. The tool is called XML Schema Definition Tool (Xsd.exe), and its for creating schema or code.

To debug further please set your msbuild logging level to /versbosity:diagnostic and see what headers (imported MSBUILD task definitions its pulling in).

This is because the C++ targets are not imported. Because XSD is not one of the default tasks defined by the managed build process. you need to add a UsingTask to reference it. Add this to your csproj file:

<UsingTask TaskName="XSD" AssemblyName="Microsoft.Build.CppTasks.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

Good Luck, Jamie

A slightly improved version/approach is to use the following in a common .targets file:

<!-- Establish the proper $(CPPTasks) location -->
<Choose>
  <When Condition=" '_$(VisualStudioVersion)'=='_11.0' " >
    <PropertyGroup>
        <CPPTasks>V110\Microsoft.Build.CPPTasks.Common.v110.dll</CPPTasks>
    </PropertyGroup>
  </When>
  <Otherwise>
    <PropertyGroup>
        <CPPTasks>Microsoft.Build.CPPTasks.Common.dll</CPPTasks>
    </PropertyGroup>
  </Otherwise>
</Choose>

Then in your .csproj you can do the following:

<UsingTask TaskName="XSD" AssemblyFile="$(VCTargetsPath)$(CPPTasks)" />

Note that this has been tested in VS2010, VS2011, VS2013 (DLL names switch back and forth depending on the version of VS installed. :(

The improvement is that only one place contains the logic for the proper DLL path and name rather than individual .csproj files.

Got it to work with a slightly different syntax. This is using VS 2012 Update 4.

What worked:

<UsingTask TaskName="XSD" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft.Cpp\v4.0\V110\Microsoft.Build.CPPTasks.Common.v110.dll" />
<Target Name="BeforeBuild">
    <Message Text="Generating serialization class(es)..." />
    <ItemGroup>
        <_SerializationFile Include="$(ProjectDir)My_Xsd_File.cs" />
    </ItemGroup>
    <XSD Condition="!Exists('@(_SerializationFile)')" GenerateFromSchema="classes" Language="CS" Namespace="$(RootNamespace)" Sources="My.Xsd.File.xsd" />
</Target>

I used the direct AssemblyFile attribute on UsingTask in my case. Note also that embedded . characters in the XSD name were converted to _ characters as well.

Add this to your project file:

<UsingTask TaskName="XSD" AssemblyName="Microsoft.Build.CppTasks.Common, Version=4.0.0.0,         Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
 <Target Name="BeforeBuild">
  <XSD Sources="MySchema.xsd" GenerateFromSchema="classes" Language="CS">
  </XSD>
 </Target>

Please look the Question Using XSD task I'm getting MSB0001: Internal MSBuild Error: xsd.exe unexpectedly not a rooted path

And add the path of xsd.exe and Microsoft.Build.CppTasks.Common.dll to Environment Variable, restart my computer. It works fine again.

The path of xsd.exe and Microsoft.Build.CppTasks.Common.dll are:

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\; C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0

Or you also can add following command line in your Pre-build event to achieve this:

xsd.exe /classes /language:CS MySchema.xsd
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!