DataContracts from XSD first!
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.
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.
Usage:
- ReStart VS2010 and add an xsd into your project.
- Select the XSD file and press F4 in order to display properties tool window.
- Change the Build Action property to contain value GenerateDataContracts
- Build the project with the XSD file. It generates the first .g.cs file.
- Chnage the view in the Solution Explorer to show all files on the file system.
- Include the new generated file in the project.
- Add reference to System.Runtime.Serialization assembly.
Enjoy.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Inject into the sequence of targets in order to add a generated file to compile -->
<PropertyGroup>
<CoreCompileDependsOn>
GenerateDataContractsFromXSD;
$(CoreCompileDependsOn);
</CoreCompileDependsOn>
</PropertyGroup>
<ItemGroup>
<SvcUtilParam Include="/nologo" />
<SvcUtilParam Include="/target:code" />
<SvcUtilParam Include="/dataContractOnly" />
<SvcUtilParam Include="/serializer:DataContractSerializer" />
<SvcUtilParam Include="/language:csharp" />
<SvcUtilParam Include="/enableDataBinding" />
<SvcUtilParam Include="/serializable" />
<SvcUtilParam Include="/internal" />
</ItemGroup>
<ItemGroup>
<AvailableItemName Include="GenerateDataContracts">
<Targets>GenerateDataContractsFromXSD</Targets>
</AvailableItemName>
</ItemGroup>
<ItemDefinitionGroup>
<GenerateDataContracts>
<!-- Use the following options to pass serialization options to SVCUTIL -->
<DataContractSchemaMapping>"/n:*,$(AssemblyName).Data"</DataContractSchemaMapping>
</GenerateDataContracts>
</ItemDefinitionGroup>
<!-- Automated Data Contract Serialisation using the SvcUtil.Exe tool -->
<!-- in order to make it automated you have to set the build tool in properties window to GenerateDataContracts -->
<Target Name="GenerateDataContractsFromXSD"
Inputs="@(GenerateDataContracts)"
Outputs="%(GenerateDataContracts.RootDir)\%(GenerateDataContracts.Directory)%(GenerateDataContracts.Filename).g.cs">
<ItemGroup>
<DataContractItems Include="@(GenerateDataContracts -> '%(FullPath)')" Condition="'%(Extension)' == '.xsd'" />
</ItemGroup>
<PropertyGroup>
<DataContractGeneratedFilePath>%(DataContractItems.RootDir)\%(DataContractItems.Directory)%(DataContractItems.Filename).g.cs</DataContractGeneratedFilePath>
<DataContractGeneratedIdentifier>@(GenerateDataContracts -> '%(RelativeDir)')%(DataContractItems.Filename).g.cs</DataContractGeneratedIdentifier>
</PropertyGroup>
<GetFrameworkSdkPath>
<Output TaskParameter="Path" PropertyName="WIN_SDK_PATH" />
</GetFrameworkSdkPath>
<Exec
Condition="'@(DataContractItems)' != ''"
Command="attrib -r "$(DataContractGeneratedFilePath)"" />
<Exec
Condition="'@(DataContractItems)' != ''"
Outputs="$(DataContractGeneratedFilePath)"
Command=""$(WIN_SDK_PATH)bin\SvcUtil.exe" @(SvcUtilParam, ' ') @(GenerateDataContracts -> '%(DataContractSchemaMapping)') "/out:$(DataContractGeneratedFilePath)" "%(DataContractItems.FullPath)"" />
</Target>
<Target Name="GetCopyGenerateDataContractItems"
AfterTargets="AssignTargetPaths">
<ItemGroup>
<DataContractItems Include="@(GenerateDataContracts -> '%(FullPath)')" Condition="'%(Extension)' == '.xsd'" />
</ItemGroup>
<AssignTargetPath Files="@(DataContractItems)" RootFolder="$(MSBuildProjectDirectory)">
<Output TaskParameter="AssignedFiles" ItemName="ContentWithTargetPath" />
</AssignTargetPath>
</Target>
</Project>