Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>DataContracts from XSD first! </p> <p>It is the modern way and very good practice, however, VS2010 has very limited automation support for it. Hence, I sat down and wrote a pure msbuild target which: does not require proj file modifications and generates .g.cs. You can also achieve generating VB code very easily with small tweaks in this file.</p> <p>Instalaltion: Copy the code and save it as GenerateDataContractsFromXSD.targets file into the folder 'C:\Program Files\MSBuild\4.0\Microsoft.Common.targets\ImportAfter'. This makes msbuild to read it each time it starts and same applies for VS2010.</p> <p>Usage:</p> <ul> <li>ReStart VS2010 and add an xsd into your project.</li> <li>Select the XSD file and press F4 in order to display properties tool window.</li> <li>Change the Build Action property to contain value GenerateDataContracts</li> <li>Build the project with the XSD file. It generates the first .g.cs file.</li> <li>Chnage the view in the Solution Explorer to show all files on the file system.</li> <li>Include the new generated file in the project.</li> <li>Add reference to System.Runtime.Serialization assembly.</li> </ul> <p>Enjoy.</p> <pre><code>&lt;Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt; &lt;!-- Inject into the sequence of targets in order to add a generated file to compile --&gt; &lt;PropertyGroup&gt; &lt;CoreCompileDependsOn&gt; GenerateDataContractsFromXSD; $(CoreCompileDependsOn); &lt;/CoreCompileDependsOn&gt; &lt;/PropertyGroup&gt; &lt;ItemGroup&gt; &lt;SvcUtilParam Include="/nologo" /&gt; &lt;SvcUtilParam Include="/target:code" /&gt; &lt;SvcUtilParam Include="/dataContractOnly" /&gt; &lt;SvcUtilParam Include="/serializer:DataContractSerializer" /&gt; &lt;SvcUtilParam Include="/language:csharp" /&gt; &lt;SvcUtilParam Include="/enableDataBinding" /&gt; &lt;SvcUtilParam Include="/serializable" /&gt; &lt;SvcUtilParam Include="/internal" /&gt; &lt;/ItemGroup&gt; &lt;ItemGroup&gt; &lt;AvailableItemName Include="GenerateDataContracts"&gt; &lt;Targets&gt;GenerateDataContractsFromXSD&lt;/Targets&gt; &lt;/AvailableItemName&gt; &lt;/ItemGroup&gt; &lt;ItemDefinitionGroup&gt; &lt;GenerateDataContracts&gt; &lt;!-- Use the following options to pass serialization options to SVCUTIL --&gt; &lt;DataContractSchemaMapping&gt;"/n:*,$(AssemblyName).Data"&lt;/DataContractSchemaMapping&gt; &lt;/GenerateDataContracts&gt; &lt;/ItemDefinitionGroup&gt; &lt;!-- Automated Data Contract Serialisation using the SvcUtil.Exe tool --&gt; &lt;!-- in order to make it automated you have to set the build tool in properties window to GenerateDataContracts --&gt; &lt;Target Name="GenerateDataContractsFromXSD" Inputs="@(GenerateDataContracts)" Outputs="%(GenerateDataContracts.RootDir)\%(GenerateDataContracts.Directory)%(GenerateDataContracts.Filename).g.cs"&gt; &lt;ItemGroup&gt; &lt;DataContractItems Include="@(GenerateDataContracts -&gt; '%(FullPath)')" Condition="'%(Extension)' == '.xsd'" /&gt; &lt;/ItemGroup&gt; &lt;PropertyGroup&gt; &lt;DataContractGeneratedFilePath&gt;%(DataContractItems.RootDir)\%(DataContractItems.Directory)%(DataContractItems.Filename).g.cs&lt;/DataContractGeneratedFilePath&gt; &lt;DataContractGeneratedIdentifier&gt;@(GenerateDataContracts -&gt; '%(RelativeDir)')%(DataContractItems.Filename).g.cs&lt;/DataContractGeneratedIdentifier&gt; &lt;/PropertyGroup&gt; &lt;GetFrameworkSdkPath&gt; &lt;Output TaskParameter="Path" PropertyName="WIN_SDK_PATH" /&gt; &lt;/GetFrameworkSdkPath&gt; &lt;Exec Condition="'@(DataContractItems)' != ''" Command="attrib -r &amp;quot;$(DataContractGeneratedFilePath)&amp;quot;" /&gt; &lt;Exec Condition="'@(DataContractItems)' != ''" Outputs="$(DataContractGeneratedFilePath)" Command="&amp;quot;$(WIN_SDK_PATH)bin\SvcUtil.exe&amp;quot; @(SvcUtilParam, ' ') @(GenerateDataContracts -&gt; '%(DataContractSchemaMapping)') &amp;quot;/out:$(DataContractGeneratedFilePath)&amp;quot; &amp;quot;%(DataContractItems.FullPath)&amp;quot;" /&gt; &lt;/Target&gt; &lt;Target Name="GetCopyGenerateDataContractItems" AfterTargets="AssignTargetPaths"&gt; &lt;ItemGroup&gt; &lt;DataContractItems Include="@(GenerateDataContracts -&gt; '%(FullPath)')" Condition="'%(Extension)' == '.xsd'" /&gt; &lt;/ItemGroup&gt; &lt;AssignTargetPath Files="@(DataContractItems)" RootFolder="$(MSBuildProjectDirectory)"&gt; &lt;Output TaskParameter="AssignedFiles" ItemName="ContentWithTargetPath" /&gt; &lt;/AssignTargetPath&gt; &lt;/Target&gt; &lt;/Project&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload