6

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

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

但是构建输出说,虽然智能感知在编辑项目文件时为我提供了 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。

*

4

5 回答 5

4

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

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

祝你好运,杰米

于 2010-04-29T16:27:03.910 回答
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 文件。

于 2015-07-24T14:16:54.350 回答
2

让它使用稍微不同的语法。这是使用 VS 2012 更新 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 名称中的嵌入字符也被转换为_字符。

于 2014-12-18T22:57:44.287 回答
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>

请查看问题使用 XSD 任务我得到 MSB0001:内部 MSBuild 错误:xsd.exe 意外不是根路径

并将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 文件 (x86)\MSBuild\Microsoft.Cpp\v4.0

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

xsd.exe /classes /language:CS MySchema.xsd
于 2014-11-03T04:34:39.583 回答
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>
于 2021-04-12T19:47:38.263 回答