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
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 through the DataColumnCollection.
Dim customers As CustomerDataSet= New CustomerDataSet()
Dim adapter As SqlDataAdapter New SqlDataAdapter( _
"SELECT * FROM dbo.Customers;", _
"Data Source=(local);Integrated " & _
"Security=SSPI;Initial Catalog=Northwind")
adapter.Fill(customers, "Customers")
Dim customerRow As CustomerDataSet.CustomersRow
For Each customerRow In customers.Customers
Console.WriteLine(customerRow.CustomerID)
Next
CustomerDataSet customers = new CustomerDataSet();
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT * FROM dbo.Customers;",
"Data Source=(local);Integrated " +
"Security=SSPI;Initial Catalog=Northwind");
adapter.Fill(customers, "Customers");
foreach(CustomerDataSet.CustomersRow customerRow in customers.Customers)
Console.WriteLine(customerRow.CustomerID);
Following is the XML Schema used for the example.
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="CustomerDataSet" xmlns:xs="https://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
Reference
DataColumnCollection Class
DataSet Class