Use the XSD Build Task in a C # Project

How can I use C ++ XSD Task in a C # project? I already created the task in the csproj file as follows:

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

but the output of the assembly says, although intellisense offers me an XSD task when editing a project file:

 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 

Oh, and I'm using Visual Studio 2010 RC.

*

+4
source share
5 answers

Forget the C ++ bit, this is a distraction from the msdn page header. The tool is called XML Schema Definition Tool (Xsd.exe) and is used to create the schema or code.

To further debug, set the msbuild logging level to / versbosity: diagnostic and see which headers (the imported MSBUILD task definitions are pulled).

0
source

This is because C ++ targets are not imported. Because XSD is not one of the default tasks defined by the managed assembly process. you need to add UseTask for its reference. 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

+4
source

A slightly improved version / approach is to use the following in a generic .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)" /> 

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

The improvement is that only one place contains logic for the correct path and name of the DLL, and not the individual .csproj files.

+3
source

I tried to work with a slightly different syntax. This is used by 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. Also note that inline characters . in the XSD name were also converted to _ characters.

+2
source

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> 

Look at the question Using the XSD task I get MSB0001: Internal error MSBuild: xsd.exe unexpectedly not root path

And add the path to xsd.exe and Microsoft.Build.CppTasks.Common.dll in the environment variable, restart the computer. It works great.

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

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

Or you can also add the following command line to the Pre-build event for this:

 xsd.exe /classes /language:CS MySchema.xsd 
+1
source

Source: https://habr.com/ru/post/1303228/


All Articles