8.1. Using XSD Schema Files to Load and Save a DataSet Structure
Problem
You need to create an
XSD schema from a
DataSet
and define the schema of a
DataSet
from an XSD schema.
Solution
Use the
XmlTextWriter
and XmlTextReader
classes.
The sample code contains three event handlers:
- Write
Button.Click
Creates a
DataSet
containing the Orders table and Order Details table from Northwind and a relation between the two. The XSD schema for theDataSet
is written both to a file and to a text box on the form.- Read
Button.Click
Creates a
DataSet
and reads in the schema from a file containing a previously serialized XSD schema. The XSD schema is written from theDataSet
to a stream and displayed.- Clear
Button.Click
Clears the
DataGrid
and the result text box.
The C# code is shown in Example 8-1.
Example 8-1. File: XsdSchemaFileForm.cs
// Namespaces, variables, and constants using System; using System.Configuration; using System.Windows.Forms; using System.Text; using System.IO; using System.Xml; using System.Xml.Schema; using System.Data; using System.Data.SqlClient; // Table name constants private const String ORDERS_TABLE = "Orders"; private const String ORDERDETAILS_TABLE = "OrderDetails"; // Relation name constants private const String ORDERS_ORDERDETAILS_RELATION = "Orders_OrderDetails_Relation"; // Field name constants private const String ORDERID_FIELD = "OrderID"; // . . . private void writeSchemaButton_Click(object sender, System.EventArgs e) { DataSet ds = new DataSet( ); SqlDataAdapter ...
Get ADO.NET Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.