在c#项目中使用XSD Build任务

时间:2010-03-05 11:53:50

标签: c# visual-studio-2010 msbuild msbuild-task

如何在c#项目中使用c ++ XSD Task?我已经在csproj文件中创建了这样的任务:

  <Target Name="BeforeBuild">
    <XSD Namespace="$(RootNamespace).Xml" Language="CS" GenerateFromSchema="classes" Sources="Xml/schema.xsd" />
  </Target>

但构建输出显示,虽然intellisense在编辑项目文件时为我提供了XSD任务:

Error   1   The "XSD" task was not found. Check the following: 
1.) The name of the task in the project file is the same as the name of the task class. 
2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with <UsingTask> in the project file, or in the *.tasks files located in the "C:\Windows\Microsoft.NET\Framework\v4.0.30128" directory. MyProject.csproj

哦,我正在使用Visual Studio 2010 RC。

*

6 个答案:

答案 0 :(得分:4)

这是因为未导入C ++目标。因为XSD不是托管构建过程定义的默认任务之一。你需要添加一个UsingTask来引用它。将其添加到您的csproj文件中:

<UsingTask TaskName="XSD" AssemblyName="Microsoft.Build.CppTasks.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
祝你好运, 杰米

答案 1 :(得分:4)

稍微改进的版本/方法是在常见的.targets文件中使用以下内容:

<!-- 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>

然后在.csproj中,您可以执行以下操作:

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

请注意,这已经在VS2010,VS2011,VS2013中进行了测试(DLL名称来回切换,具体取决于安装的VS版本。:(

改进之处在于只有一个地方包含正确的DLL路径和名称的逻辑,而不是单独的.csproj文件。

答案 2 :(得分:2)

使用略有不同的语法。这是使用VS 2012 Update 4。

什么有效:

<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>

在我的案例中,我使用AssemblyFile上的直接UsingTask属性。另请注意,XSD名称中的嵌入式.字符也会转换为_个字符。

答案 3 :(得分:1)

将其添加到项目文件中:

<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>

请查看问题Using XSD task I'm getting MSB0001: Internal MSBuild Error: xsd.exe unexpectedly not a rooted path

将xsd.exe和Microsoft.Build.CppTasks.Common.dll的路径添加到环境变量,重新启动计算机。它再次正常工作。

xsd.exe和Microsoft.Build.CppTasks.Common.dll的路径是:

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

或者您也可以在Pre-build事件中添加以下命令行来实现此目的:

xsd.exe /classes /language:CS MySchema.xsd

答案 4 :(得分:1)

看起来 XSD MsBuild 任务自 VS2017 以来已被弃用。以下目标将在编译前使用 Exec 任务调用 Xsd.exe。

 <!--
      Executes the xsd.exe command on Filename.xsd.  If more XSD files
      are required, this target should be modified to find and generate 
      classes for all XSD classes, or alternatively use an MsBuild item
      to define which XSD files to use.
   -->
 <Target Name="Xsd" BeforeTargets="Compile" Inputs="Folder\Filename.xsd" Outputs="Folder\Filename.cs">
    <Message Importance="high" Text="Executing Xsd.exe on Folder\Filename.xsd"/>
    <!--
      Note the XSD msuild task is deprecated and removed.
      So execute the command using the Exec task.
     -->
    <Exec Command="&quot;$(SDK40ToolsPath)Xsd.exe&quot; /classes &quot;Folder\Filename.xsd&quot; -outputdir:Folder /namespace:Root.Folder"/>
  </Target>

答案 5 :(得分:0)

忘记C ++位,这是对msdn页面标题的干扰。该工具称为XML Schema Definition Tool (Xsd.exe),用于创建模式或代码。

要进一步调试,请将您的msbuild日志记录级别设置为 / versbosity:diagnostic ,并查看哪些标头(导入的MSBUILD任务定义)。