14.18 Restoring a DataSet from an XML File

 <  Day Day Up  >  

14.18 Restoring a DataSet from an XML File

You want to restore the data within an XML file previously generated by a DataSet back into another DataSet object.


Technique

In the previous recipe, the WriteXml method defined in the DataSet class generated an XML file containing the current state of the data within the DataSet . This XML file also supported the generation of an inline schema as well as a special file format called a DiffGram containing the original values of any modified records. To restore the DataSet using the data within that generated XML file, call the ReadXml method. The parameters to this method are similar to the WriteXml method. The first parameter is a string denoting the file path of the XML file to load. The second parameter is a value from the XmlReadMode enumerated data type. This enumeration contains a few more values than the XmlWriteMode enumeration. The similar values include XmlReadMode.DiffGram , which reads in the current values as well as the values of any records that were modified. It allows you to then accept or reject the changes within the new DataSet by calling the AcceptChanges or RejectChanges methods . The XmlReadMode.ReadSchema reads the inline schema contained within the XML file. You can optionally allow the method to infer the organization of data by using XmlReadMode.InferSchema . If you use this value, the inline schema, if present, is ignored. The XmlReadMode.IgnoreSchema also ignores the schema, but if the DataSet already contains a schema from a ReadSchema method call, the XML file is validated using that schema and throws an exception if the file is invalid. Finally, XmlReadMode.Auto automatically selects the best XmlReadMode based on the format of the XML file. The following example continues the application created in the last section:

 
 private void mnuOpen_Click(object sender, System.EventArgs e) {     if( productsDS1.HasChanges() )     {         if( AskUser("The Dataset has changed. Continue?") == DialogResult.No )         {             return;         }     }     if( openFileDialog1.ShowDialog() == DialogResult.OK )     {         productsDS1.Clear();         productsDS1.ReadXml( openFileDialog1.FileName, XmlReadMode.Auto );         dataGrid1.SetDataBinding( productsDS1, "Products" );     } } 

Comments

The ReadXml method presents an interesting twist on using a DataSet object. If you were to take an informal poll and ask people to define a DataSet , most would probably say that it is used in a disconnected environment to store tables and data extracted from a database. Although it is certainly true, it doesn't fully list all the possible scenarios for the different uses a DataSet has. A DataSet can read an XML file, logically deduce how to best organize that data, and even automatically generate a schema from that data. There is in fact no requirement that you have to use a DataSet to store data that comes from a database. By using WriteXml and ReadXml , you can create a data-storage mechanism for your application that works solely with XML files, rather than make trips to a database server. By pondering the possibilities of the different ways to exploit the DataSet class, you'll undoubtedly come up with some interesting uses if you realize a database doesn't always have to be in the equation.

 <  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