Generate Typed DataSet from XSD file

Tags: XSD, XML

XSD stands for XML Schema Definition which basically describes the structure of an XML document. You can learn in detail about XSD at

Now, as we know the basic about XSD, lets consider that we have the following:

  • An XSD file which contains the structure of the XML document

         <?xml version="1.0"?>
            <xsd:schema xmlns:xsd="">
               <xsd:element name="Product">
                          <xsd:element name="ProductName" type="xsd:string"/>
                      <xsd:attribute name="ProductID" use="required" type="xsd:int"/>

  • Data that should be part of the XML document

and we have to create an XML document using the XSD file and the data.

As we want to create an XML out of the XSD, the best approach will be to get a DataSet with the structure in XSD file, then assign the data that we have to the DataSet and generate the XML using the DataSet.

Microsoft provides 2 ways using which we can get a Typed DataSet from an XSD file and use it further in our code.

XSD.exe provided with .Net Framework SDK

XSD.exe can used to generate the CLR classes for XSD files. It is part of .Net Framework SDK and can be found under InstallationPath\Microsoft.NET\SDK\v2.0 64bit\Bin. Use the command below to generate a CS file for the XSD in our example.

xsd /dataset /language:CS XSDSchemaFile.xsd

 You can read more about XSD.exe at


This is a faster and my favorite approach for generating a CS file for the XSD. Here is what you need to do:



  Add the XSD file to the solution and go to its Properties

  Set the Build Action to either None or Content

  Set the Custom Tool to MSDataSetGenerator

  That’s it. Visual Studio generates the DataSet CS file for you. You will find a file XSDSchemaFile.Designer.cs


We can check the auto-generated code and start using the class in our application. If you notice the class generated is derived from System.Data.DataSet. Now lets start using the Typed DataSet in our application, assign it the data and generate the XML file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace XSD_DataSet_XML
    class Program
        static void Main(string[] args)
            NewDataSet ds = new NewDataSet(); //Auto-Generated Class is named as NewDataSet
            DataRow dr = ds.Tables["Product"].NewRow(); //DataRow to be added to DataSet

            dr["ProductID"] = "1";
            dr["ProductName"] = "XBox";



Hope this helps. Feel free to drop a comment.

Add a Comment