Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate DataContract from XSD

I want to be able to generate a DataContract from a XSD file, preferably using the xsd.exe tool. What is the easiest way for it to auto generate the [DataContract] and [DataMember] on each of my items?

Or is there a better approach? I am trying to avoid having to recreate the data contract each time the XSD file is changed and regenerated.

like image 400
Daveo Avatar asked Feb 03 '10 00:02

Daveo


2 Answers

The xsd.exe tool predates WCF and doesn't know anything about [DataContract] and [DataMember]. If you do use xsd.exe, you'll have to switch WCF to use the XmlSerializer instead of its default DataContractSerializer for serializing the data contracts.

The WCF equivalent for xsd.exe is svcutil.exe - it has a parameter /dconly which creates the data contracts only, from a given XSD file. This will generate a C# or VB.NET file for you, containing the data contracts nicely annotated.

Usage:

svcutil.exe (name of your XSD).xsd /dconly 

This would generate a *.cs file by the same base name in your directory.

In my experience, svcutil.exe is quite picky about its XML structures - so don't be surprised if it barks at you with tons of warnings and/or errors.

like image 70
marc_s Avatar answered Sep 24 '22 17:09

marc_s


Use svcutil.exe instead of xsd.exe

How to Use ? Go to Start Menu --> Microsoft Visual Studio 2008 --> Visual Studio Tools --> Visual Studio 2008 Command Prompt

and Change the directoy you want or change the directory to whre your xsd is there.

svcutil.exe /help  

it will list all the options.

one of the option I use to generate data contarct only is

svcutil.exe /target:code /n:*,[Your Company and Department].Common.DataTransferObjects /dataContractOnly /serializer:auto /importXmlTypes common.xsd /out:common.cs 

Keep coding hava good day!

like image 42
zakirhas7 Avatar answered Sep 22 '22 17:09

zakirhas7