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
DataSetand anXmlDataDocumentusing one of three methods specified by the user. The default view for the Orders table of theDataSetis bound to the data grid on the form and the contents of theXmlDataDocumentare displayed in the text box.- Clear
Button.Click Clears the contents of the data grid displaying the
DataSetand the text box displaying the contents of theXmlDataDocument.FillDataSet( )This method loads the
DataSetwith 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"; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access