[ Team LiB ]Recipe 8.2 Saving and Loading a DataSet from XML
Problem
You need to save a DataSet as an XML file and create a DataSet from an XML file.
Solution
Use the XmlTextWriter and XmlTextReader classes.
The sample code contains three event handlers:
Write Button.Click
Creates a DataSet containing the Orders and Order Details tables from Northwind
and a relation between the two tables. The XML schema and data for the DataSet
is written both to a file and to a text box on the form.
Read Button.Click
Creates a DataSet and reads in schema and data from a file containing a previously
serialized XML. The XML is written from the DataSet to a stream and displayed.
Clear Button.Click
Clears the data grid and the result text box.
The C# code is shown in Example 8-3
.
Example 8-3. File: XmlFileForm.cs
// Namespaces, variables, and constants
using System;
using System.Configuration;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Data;
using System.Data.SqlClient;
ConfigurationSettings.AppSettings["Sql_ConnectString"]);
DataTable orderDetailTable = new DataTable(ORDERDETAILS_TABLE);
da.FillSchema(orderDetailTable, SchemaType.Source);
da.Fill(orderDetailTable);
ds.Tables.Add(orderDetailTable);
// Create a relation between the tables.
ds.Relations.Add(ORDERS_ORDERDETAILS_RELATION,
ds.Tables[ORDERS_TABLE].Columns[ORDERID_FIELD],
ds.Tables[ORDERDETAILS_TABLE].Columns[ORDERID_FIELD],
true);
// Bind the default view of the Orders table to the grid.
resultDataGrid.DataSource = ds.Tables[ORDERS_TABLE].DefaultView;
// Write the XSD schema and data to a file.
// Display file dialog to select XML file to write.
SaveFileDialog sfd = new SaveFileDialog( );
sfd.InitialDirectory = System.IO.Path.GetTempPath( );
sfd.Filter = "XML Files (*.xml)|*.xml|All files (*.*)|*.*";
sfd.FilterIndex = 1;
if (sfd.ShowDialog( ) == DialogResult.OK)
{
FileStream fs = new FileStream(sfd.FileName, FileMode.Create,
FileAccess.Write);
// Create an XmlTextWriter using the file stream.
XmlTextWriter xtw = new XmlTextWriter(fs, Encoding.Unicode);
try
// Create an XmlTextReader using the file stream.
XmlTextReader xtr = new XmlTextReader(fs);
try
{
// Read the schema into the DataSet.
DataSet ds = new DataSet( );
ds.ReadXml(xtr, XmlReadMode.ReadSchema);
// Bind the default view of the Orders table to the grid.
resultDataGrid.DataSource =
ds.Tables[ORDERS_TABLE].DefaultView;
// Write the XML to a memory stream and display it.
MemoryStream ms = new MemoryStream( );
ds.WriteXml(ms, XmlWriteMode.WriteSchema);
byte[] result = ms.ToArray( );
ms.Close( );
resultTextBox.Text = Encoding.UTF8.GetString(result, 0,
result.Length);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
xtr.Close( );
}
The DataSet is written with an inline XSD schema for the relational
structure of the DataSet.
If an in-line schema is not written, the ReadXml( ) method can still be used to read the
data into a DataSet, but the method will not be able to completely recreate the schema for
the DataSet.
The XmlRead( ) method takes an optional argument that specifies a value from the
XmlReadMode enumeration described in Table 8-3
.
Table 8-3. XmlReadMode enumeration
Value Description
Auto
Uses the most appropriate of the following settings:
•
DiffGram if the data is a DiffGram
•
ReadSchema if the DataSet already has a schema or the XML
document contains an inline schema
•
InferSchema if the DataSet does not already have a schema and