Synchronizing a DataSet with an XML Document

Problem

You need to work with both a DataSet and its XML representation.

Solution

Use a synchronized DataSet and XmlDataDocument .

The sample code contains two event handlers and one method:

Go Button.Click

Synchronizes a DataSet and an XmlDataDocument using one of three methods specified by the user . The default view for the Orders table of the DataSet is bound to the data grid on the form and the contents of the XmlDataDocument are displayed in the text box.

Clear Button.Click

Clears the contents of the data grid displaying the DataSet and the text box displaying the contents of the XmlDataDocument .

FillDataSet()

This method loads the DataSet with a subset of the Orders and Order Details data from Northwind and creates a relation between the tables.

The C# code is shown in Example 8-5.

Example 8-5. File: SyncDataSetWithXmlDocForm.cs

// Namespaces, variables, and constants using System; using System.Configuration; using System.Windows.Forms; using System.Xml; 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 const String XMLFILENAME = ConfigurationSettings.AppSettings["Project_Directory"] + @"Chapter 08Orders_OrderDetails.xml"; // . . . private void goButton_Click(object sender, System.EventArgs e) { Cursor.Current = Cursors.WaitCursor; DataSet ds = null; XmlDataDocument xmlDoc = null; if (method1RadioButton.Checked) { // Load DataSet with schema and data. ds = FillDataSet(true); // Get the XML document for the DataSet. xmlDoc = new XmlDataDocument(ds); } else if(method2RadioButton.Checked) { // Create DataSet with schema, but no data. ds = FillDataSet(false); // Get the XML document for the DataSet. xmlDoc = new XmlDataDocument(ds); // Load the data into the XML document from the XML file. xmlDoc.Load(XMLFILENAME); } else if(method3RadioButton.Checked) { // Create an XML document. xmlDoc = new XmlDataDocument(); // Get the DataSet for the XML document. ds = xmlDoc.DataSet; // Get schema for the DataSet from the XSD inline schema. ds.ReadXmlSchema(XMLFILENAME); // Load the data into the XML document from the XML file. xmlDoc.Load(XMLFILENAME); } // Display the XML data. resultTextBox.Text = xmlDoc.OuterXml; // Bind the DataSet to the grid. dataGrid.DataSource = ds.Tables[ORDERS_TABLE].DefaultView; Cursor.Current = Cursors.Default; } private DataSet FillDataSet(bool includeData) { DataSet ds = new DataSet("Orders_OrderDetails"); SqlDataAdapter da; // Fill the Order table and add it to the DataSet. da = new SqlDataAdapter("SELECT * FROM Orders", ConfigurationSettings.AppSettings["Sql_ConnectString"]); DataTable orderTable = new DataTable(ORDERS_TABLE); da.FillSchema(orderTable, SchemaType.Source); if (includeData) da.Fill(orderTable); ds.Tables.Add(orderTable); // Fill the OrderDetails table with schema and add it to the DataSet. da = new SqlDataAdapter("SELECT * FROM [Order Details]", ConfigurationSettings.AppSettings["Sql_ConnectString"]); DataTable orderDetailTable = new DataTable(ORDERDETAILS_TABLE); da.FillSchema(orderDetailTable, SchemaType.Source); if (includeData) da.Fill(orderDetailTable); ds.Tables.Add(orderDetailTable); // Create a relation between the tables. ds.Relations.Add(ORDERS_ORDERDETAILS_RELATION, ds.Tables[ORDERS_TABLE].Columns[ORDERID_FIELD], ds.Tables[ORDERDETAILS_TABLE].Columns[ORDERID_FIELD], true); return ds; }

Discussion

The .NET Framework allows real-time, synchronous access to both a DataSet and its XML representation in an XmlDataDocument object. The synchronized DataSet and XmlDataDocument work with a single set of data.

The solution shows three ways to synchronize a DataSet with an XmlDataDocument :

Method 1

Populate a DataSet with both schema and data. Synchronize it with a new XmlDataDocument , initializing it with the DataSet in the constructor.

Method 2

Populate a DataSet with a schema but no data. Synchronize it with a new XmlDataDocument , initializing it with the DataSet in the constructor. Load an XML document into the XmlDataDocument . The table and column names in the DataSet schema to be synchronized must match those in the XmlDataDocument .

Method 3

Create a new XmlDataDocument and access its DataSet through the DataSet property. Populate the schema for the DataSet . In the example, the schema is read from the XSD inline schema in the XML document. If the XML document does not have an inline schema, it might be possible to infer the schema using the InferSchema() method. Otherwise, the entire DataSet schema must be defined programmatically. Next, load the XML document into the synchronized XmlDataDocument . The table and column names in the DataSet schema to be synchronized must match those in the XmlDataDocument .

Example 8-6 shows the XML file used in this solution.

Example 8-6. Orders with Order Details XML file, with schema

10248 VINET 5 1996-07-04T00:00:00.0000000-04:00 1996-08-01T00:00:00.0000000-04:00 1996-07-16T00:00:00.0000000-04:00 3 32.38 Vins et alcools Chevalier 59 rue de l'Abbaye Reims 51100 France 10249 TOMSP 6 1996-07-05T00:00:00.0000000-04:00 1996-08-16T00:00:00.0000000-04:00 1996-07-10T00:00:00.0000000-04:00 1 11.61 Toms Spezialitten Luisenstr. 48 Mnster 44087 Germany 10250 HANAR 4 1996-07-08T00:00:00.0000000-04:00 1996-08-05T00:00:00.0000000-04:00 1996-07-12T00:00:00.0000000-04:00 2 65.83 Hanari Carnes Rua do Pao, 67 Rio de Janeiro RJ 05454-876 Brazil 10248 11 14 12 0 10248 42 9.8 10 0 10248 72 34.8 5 0 10249 14 18.6 9 0 10249 51 42.4 40 0 10250 41 7.7 10 0 10250 51 42.4 35 0.15 10250 65 16.8 15 0.15

Категории