8.8. Creating an XML File That Shows Changes Made to a DataSet
Problem
When you use the GetXML( ) method of the
DataSet, you may see only the current values in
the DataSet. You want to get the original
values
and see which rows were added, edited,
or deleted.
Solution
Create an XML DiffGram, which
is a document that details the modifications made to a
DataSet.
The sample code contains two event handlers and a single method:
Form.LoadSets up the sample by loading the Categories table from Northwind into a
DataSetand displays theDiffGramfor theDataSetbefore modification.- Make Changes
Button.Click Deletes the first row, modifies the second row, inserts a row before the first row, and adds a row to the end of the Categories
DataTableDisplayDiffGram( )This method outputs the
DiffGramfor theDataSetto both a file and to atextboxon the form.
The C# code is shown in Example 8-12.
Example 8-12. File: XmlDiffgramForm.cs
// Namespaces, variables, and constants using System; using System.Configuration; 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 CATEGORIES_TABLE = "Categories"; // Field name constants private const String CATEGORYNAME_FIELD = "CategoryName"; private const String XMLDIFFGRAMFILENAME = ConfigurationSettings.AppSettings["Temp_Directory"] + "CategoriesDiffgram.xml"; private DataSet ds; // . . . private void XmlDiffgramForm_Load(object sender, System.EventArgs ...
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