9.5. Synchronizing a DataSet and an XML Document
Problem
You need to simultaneously work with both a DataSet
and its XML representation, keeping them automatically synchronized with any changes made.
Solution
Use a DataSet
and XmlDataDocument
together as demonstrated in the three approaches.
The solution loads a DataSet
with a subset of the Production.Product
and Production.Location
tables in the AdventureWorks
database and creates a data relation between them on the ProductID
column. The solution demonstrates three methods to create a synchronized DataSet
and XML document, as described in the discussion section. DataSet
and XML document metrics are output throughout to show the effect of the various aspects of the synchronization for each of the three approaches.
The C# code in Program.cs in the project file SynchronizeDataWithXmlDocument
is shown in Example 9-7.
Example 9-7. File: Program.cs for SynchronizeDataWithXmlDocument solution
using System; using System.Data; using System.Data.SqlClient; using System.Xml; namespace SynchronizeDataSetWithXmlDocument { class Program { static void Main(string[] args) { string xmlFileName = @"..\..\..\ProductInventory.xml"; DataSet ds; XmlDataDocument xd; Console.WriteLine("---Fill DataSet with schema and data; " + "Get XmlDataDocument based on DataSet---"); ds = null; xd = null; // Fill the DataSet with schema and data ds = FillDataSet(true); OutputInfo(ds, xd); // Synchronize the XmlDataDocument to the DataSet xd = new XmlDataDocument(ds); OutputInfo(ds, ...
Get ADO.NET 3.5 Cookbook, 2nd Edition 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.