14.17 Saving DataSet Information to an XML File

 <  Day Day Up  >  

14.17 Saving DataSet Information to an XML File

You want to save the current state of a DataSet object to an XML file.


Technique

A DataSet maintains internal data using a hierarchical organization scheme that lends itself well to creating an XML representation. To save a DataSet to an XML file, call the WriteXml method, passing a string in the first parameter designating the filename and a value from the XmlWriteMode enumerated data type. The possible values for this parameter include XmlWriteMode.WriteSchema , which includes the schema inline with the document; XmlWriteMode.IgnoreSchema , which simply writes the XML data only; and XmlWriteMode.DiffGram . A DiffGram not only contains the current state of each record in the DataSet but also contains the original values of each modified record. Listing 14.6 demonstrates a database-editing application that lets you save the current state of a DataSet to an XML file without updating the underlying data source. The next section expands on this example, further showing how to reload the DataSet from the XML file.

Listing 14.6 Saving a DataSet to XML
 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace _14_DataSetToXml {     /// <summary>     /// Summary description for Form1.     /// </summary>     public class Form1 : System.Windows.Forms.Form     {         private System.Windows.Forms.MainMenu mainMenu1;         private System.Windows.Forms.MenuItem menuItem1;         private System.Windows.Forms.MenuItem mnuOpen;         private System.Windows.Forms.MenuItem mnuSave;         private System.Windows.Forms.MenuItem mnuExit;         private System.Windows.Forms.MenuItem menuItem5;         private System.Windows.Forms.MenuItem mnuLoad;         private System.Windows.Forms.MenuItem mnuUpdate;         private System.Windows.Forms.DataGrid dataGrid1;         private System.Data.SqlClient.SqlCommand sqlSelectCommand1;         private System.Data.SqlClient.SqlCommand sqlInsertCommand1;         private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;         private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;         private System.Data.SqlClient.SqlConnection sqlConnection1;         private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;         private _14_DataSetToXml.ProductsDS productsDS1;         private System.Windows.Forms.SaveFileDialog saveFileDialog1;         private System.Windows.Forms.OpenFileDialog openFileDialog1;         /// <summary>         /// Required designer variable.         /// </summary>         private System.ComponentModel.Container components = null;         public Form1()         {             InitializeComponent();             // fill the dataset and bind to datagrid             sqlDataAdapter1.Fill( productsDS1 );             dataGrid1.SetDataBinding( productsDS1, "Products" );         }         protected override void Dispose( bool disposing )         {             if( disposing )             {                 if (components != null)                 {                     components.Dispose();                 }             }             base.Dispose( disposing );         }          /* Windows Forms Designer Generated Code */         [STAThread]         static void Main()         {             Application.Run(new Form1());         }         private void mnuLoad_Click(object sender, System.EventArgs e)         {             if( productsDS1.HasChanges() )             {                 if( AskUser("The Dataset has changed. " +                     " Are you sure you want to continue? ")==DialogResult.No )                 {                     return;                 }             }             // clear dataset and get data from database             productsDS1.Clear();             sqlDataAdapter1.Fill( productsDS1 );             dataGrid1.SetDataBinding( productsDS1, "Products" );         }         private DialogResult AskUser(string question)         {             return MessageBox.Show( question, "DatasetToXml",                 MessageBoxButtons.YesNo, MessageBoxIcon.Question );         }         private void mnuSave_Click(object sender, System.EventArgs e)         {             if( saveFileDialog1.ShowDialog() == DialogResult.OK )             {                 productsDS1.WriteXml( saveFileDialog1.FileName,                     XmlWriteMode.DiffGram);             }         }         private void mnuUpdate_Click(object sender, System.EventArgs e)         {             if( productsDS1.HasChanges() )             {                 // update the database                 sqlDataAdapter1.Update( productsDS1, "Products" );             }         }         private void mnuOpen_Click(object sender, System.EventArgs e)         {              // implemented in next section         }         private void mnuExit_Click(object sender, System.EventArgs e)         {             this.Close();         }     } } 

Comments

Being able to save an entire DataSet to an XML file has advantages, especially when transferring the DataSet across machine boundaries. Additionally, you can take advantage of DataSet -generated XML files within ASP.NET. Although not covered until Chapter 16, "ASP.NET," the following technique is worth mentioning here. When an ASP.NET page that utilizes a database connection loads, you need to fill a DataSet . However, once the final response is sent back to the client, the DataSet is lost because you cannot have instance data within an ASP.NET application persist across subsequent page loads. You can utilize a special dictionary-based collection called Session . However, placing a DataSet object into the collection for each connected user can very well fill up entire amounts of memory on your server. To combat this issue, use the technique described in this section by writing the XML representation of the DataSet object and read it using the technique in the next section each time your page loads. Not only does it allow you to persist the changed data across subsequent page loads, but it also gives a slight performance increase because you don't need to constantly connect and transfer data from the database server.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net