Generating Strongly Typed DataSets
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 Windows Software Development Kit (SDK).
The following code shows the syntax for generating a DataSet using this tool.
xsd.exe /d /l:CS XSDSchemaFileName.xsd /eld /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
/eld directive specifies that you can use LINQ to DataSet to query against the generated DataSet. This option is used when the
/d option is also specified. For more information, see Querying Typed DataSets. 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
/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
The following code shows the syntax for accessing the namespace passed to XSD.exe in an ADO.NET application.
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.
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="" 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>