XSD ( XSD tool / XSD.exe ) To generate .Net Class file

1) We can use XSD Tool ( Xml Schema Definition Tool ) or XSD.exe to generate class files (.cs or .vb ) as per your requirments from Typed Datasets. XSD.exe is the XML Schema Definition tool which generates XML schema or common language runtime classes from XDR, XML, and XSD files.
The file locates at C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\XSD.exe
OR
C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin Depends up on your version of .Net.
If you are having Express edition of ( V 2.0) then it might possible that you don’t have .Net command prompt option available in Start->Programs , then you have to go command prompt and goto location
“C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin”
and then type following with location of .xsd file on prompt
xsd.exe -c -l:c# C:\MyProjects\Employee\EmpTableDataSet.xsd

( In my case thr are 3 datasets thr can be one or many depends upon your need.)

dataset11

you will find class file “EmpTableDataSet.cs” generated at
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin
you can change output directory pls chk the options available given below

2 ) This tool can be also be used to generate xml schema file (.xsd) from an xml data file (.xml). Suppose we have file Catelog.xml as shown ,then we can generate XSD files as below from Command prompt.
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>xsd.exe C:\CatelogOne.Xml

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>xsd.exe C:\CatelogOne.Xml Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file ‘C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\CatelogOne.xsd’.

3 )To generate schema from a compiled class library ( .dll)
C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>xsd C:\BLLTable.dll
on enter it will generate the schema of the various types present in the dll.
( Consider Class library you created in project or any other )

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin>xsd C:\BLLTable.dll
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 2.0.50727.42]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file ‘C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\schema0.xsd’.

Imp References – XSD from msdn & Bug in XSD

Options Available for XSD ( you will get it on command prompt by Xsd /? )

xsd.exe -
Utility to generate schema or class files from given source.

xsd.exe .xsd /classes|dataset [/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’.

/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:’.

- ARGUMENTS -
.xsd Name of a schema containing elements to import.
.dll|exe Name of an assembly containing types to generate schema for.
.xml Name of an xml file to infer xsd schema from.
.xdr Name of an xdr schema to convert to xsd.
Multiple file arguments of the same type may be provided.

About these ads

9 responses to “XSD ( XSD tool / XSD.exe ) To generate .Net Class file

  1. Hi,

    i have created a VB-class-file from an existing xsd.
    Did you know about a possibility how to create a XML
    which is defined through the VB-class. ?

    • collecteddotnet

      Hi tensor,
      I am not sure what r u talking about. Could u please elaborate what actually your scenario is.
      But if you want to create xml file from VB use XmlDocument and its methods & related properties.

      • Steven Williams

        tensor,

        I think what you are looking for is serialization.
        below is a simple example.

        1) create the class – there are a couple of methods to serialization this method needs you to put the ( _ ) above the class

        _
        Partial Public Class feedAuthor

        Private nameField As String

        Private uriField As String

        ”’
        Public Property name() As String
        Get
        Return Me.nameField
        End Get
        Set(ByVal value As String)
        Me.nameField = value
        End Set
        End Property

        ”’
        Public Property uri() As String
        Get
        Return Me.uriField
        End Get
        Set(ByVal value As String)
        Me.uriField = value
        End Set
        End Property
        End Class

        2) create a serialization method

        Public Shared Function Serialize(ByVal pcObject As Object) As String
        Dim lcSerializer As New System.Xml.Serialization.XmlSerializer(pcObject.GetType)
        Dim lcMemStream As New System.IO.MemoryStream
        lcSerializer.Serialize(lcMemStream, pcObject)
        lcMemStream.Position = 0
        Dim lcSR As New System.IO.StreamReader(lcMemStream)
        Dim lstString As String = lcSR.ReadToEnd
        lcMemStream.Close()
        lcMemStream.Dispose()
        Return lstString
        End Function

        3) serialize the object

        Dim objfeedauthor as new feedauthor()
        Dim xml as String = Serialize(objfeedauthor)

  2. Pingback: links for 2009-11-25 « Praveen’s Blog

  3. Pingback: XSD Utility | skguru

  4. Really appreciate your help…. it quickly created the cs data file I needed.

    Cheers

  5. When some one searches for his essential thing, so he/she wants to be available that in detail,
    thus that thing is maintained over here.

  6. Pingback: Typed DataSet and Untyped DataSet | CollectedDotNet

  7. Hi, just wanted to say, I enjoyed this blog post. It was practical.
    Keep on posting!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s