Thursday, August 7, 2008

Generate C# file from XML Schema

To write a serializable C# class file based on an XML Schema is a pain, in case of a complex schema implementation. What if we can have C# class file generated for us? Visual Studio provides us with a tool (XSD.exe) which can do the needful.

Step 1: Go to run and type:

Visual Studio 2005: %comspec% /k ""C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat"" x86

Visual Studio 2008: %comspec% /k ""c:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat"" x86

This will open up a command prompt window with current directory as "C:\WINDOWS\system32"

Step 2: Navigate to directory where your schema file (.xsd) is. (This step is added for sake of simplicity)

C:\WINDOWS\system32>cd c:\myschema

Step 3: Punch in following line in command prompt

xsd.exe -c -l:c# -n:myNameSpace schema.xsd

This will create a serialized C# class file (.cs) in the same directory as you schema

=================== XSD.exe HELP ============
xsd.exe - Utility to generate schema or class files from given source.
xsd.exe .xsd /classesdataset [/e:] [/l:] [/n:] [/o:] [/s] [/uri:]
xsd.exe .dll.exe [/outputdir:] [/type: [...]]
xsd.exe .xml [/outputdir:]
xsd.exe .xdr [/outputdir:]

- OPTIONS -

/classes
Generate classes for this schema. Short form is '/c'.

/dataset
Generate sub-classed DataSet for this schema. Short form is '/d'.

/enableLinqDataSet
Generate LINQ-enabled sub-classed Dataset for the schemas provided. Short form is '/eld'.

/element:
Element from schema to process. Short form is '/e:'.

/fields
Generate fields instead of properties. Short form is '/f'.

/order
Generate explicit order identifiers on all particle members.

/enableDataBinding
Implement INotifyPropertyChanged interface on all generated types
to enable data binding. Short form is '/edb'.

/language:
The language to use for the generated code. Choose from 'CS', 'VB', 'JS',
'VJS', 'CPP' or provide a fully-qualified name for a class implementing
System.CodeDom.Compiler.CodeDomProvider. The default language
is 'CS' (CSharp). Short form is '/l:'.

/namespace:
The namespace for generated class files. The default namespace
is the global namespace. Short form is '/n:'.

/nologo
Suppresses the banner.

/out:
The output directory to create files in. The default
is the current directory. Short form is '/o:'.

/type:
Type from assembly to generate schema for. Multiple types may be provided.
If no types are provided, then schemas for all types in an assembly
are generated. Short form is '/t:'.

/uri:
Uri of elements from schema to process. Short form is '/u:'.

- ADVANCED -

/parameters:
Read command-line options from the specified xml file. Short form is '/p:'.

=========================================

2 comments:

nikhil rajan said...

Thnx Man It workd fro me

Mau said...

Nice, I was having errors with xsd2code.
Wonderful tip, thanks a lot!