Generating a Strongly Typed DataSet

Given an XML Schema that complies with the XML Schema definition language (XSD) standard, you can generate a strongly typed DataSet using the XSD.exe tool provided with the .NET Framework SDK.

The following code shows the syntax for generating a DataSet using this tool.

xsd.exe /d /l:CS XSDSchemaFileName.xsd /n:XSDSchema.Namespace

In this syntax, the /d directive tells the tool to generate a DataSet, and the /l: tells the tool what language to use (for example, C# or Visual Basic .NET). The optional /n: directive tells the tool to also generate a namespace for the DataSet called XSDSchema.Namespace. The output of the command is XSDSchemaFileName.cs, which can be compiled and used in an ADO.NET application. The generated code can be compiled as a library or a module.

The following code shows the syntax for compiling the generated code as a library using the C# compiler (csc.exe).

csc.exe /t:library XSDSchemaFileName.cs /r:System.dll /r:System.Data.dll

The /t: directive tells the tool to compile to a library, and the /r: directives specify dependent libraries required to compile. The output of the command is XSDSchemaFileName.dll, which can be passed to the compiler when compiling an ADO.NET application with the /r: directive.

The following code shows the syntax for accessing the namespace passed to XSD.exe in an ADO.NET application.

Imports XSDSchema.Namespace
[C#]
using XSDSchema.Namespace;

The following code example uses a typed DataSet named CustomerDataSet to load a list of customers from the Northwind database. Once the data is loaded using the Fill method, the example loops through each customer in the Customers table using the typed CustomersRow (DataRow) object. This provides direct access to the CustomerID column, as opposed to accessing it through the DataColumnCollection.

Dim custDS As CustomerDataSet= New CustomerDataSet()
Dim custCMD As SqlDataAdapter New SqlDataAdapter("SELECT * FROM Customers", _
                                                 "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")

custCMD.Fill(custDS, "Customers")

Dim custRow As CustomerDataSet.CustomersRow
For Each custRow In custDS.Customers
  Console.WriteLine(custRow.CustomerID)
Next
[C#]
CustomerDataSet custDS = new CustomerDataSet();
SqlDataAdapter custCMD = new SqlDataAdapter("SELECT * FROM Customers",
                                            "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");

custCMD.Fill(custDS, "Customers");

foreach(CustomerDataSet.CustomersRow custRow in custDS.Customers)
  Console.WriteLine(custRow.CustomerID);

Following is the XML Schema used for the example.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="CustomerDataSet"  xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="CustomerDataSet" msdata:IsDataSet="true">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="Customers">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="CustomerID" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

See Also

Working with a Typed DataSet | Creating and Using DataSets | DataColumnCollection Class | DataSet Class