8.3. 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 08\Orders_OrderDetails.xml"; ...

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.