3. Reading and Writing a Data Set as XML
A database is not the only
source for or destination of data set data. XML files can also be the
storage media for the data. Unlike database access, which requires a
separate provider-specific class to perform the transfer, XML file I/O
is done by the DataSet class itself.
After all, an XML file is a text file of information in a standard
format; therefore, access to it is provided in a standard manner by
derived classes of the Stream class.
The DataSet class has four methods for performing XML file I/O:
WriteXml: writes the contents of a data set as XML to a file
WriteXmlSchema: writes the structure of the data set as an XML schema to a file
ReadXml: reads the contents of an XML file into a data set
ReadXmlSchema: reads the contents of an XML schema file and builds the data set structure from it
The XML schema is translated to and from Constraint and DataRelation objects in the data set, as well as the DataTable objects. Unlike relational data, which is flat in structure, XML is hierarchical. The DataRelation
classes are the mechanism for expressing the hierarchy within the data
set. For instance, the following code, which we used earlier in Listing 2, creates a DataRelation object that specifies the parent/child relationship between the Categories and Products tables:
// Add relation to the DataSet.
This relationship has been in our data set ever since we first built the data set. If we now execute the WriteXml method, as shown in the following code excerpt, and then view the contents of the file, we can see the generated XML (see Figure 4):
// The XML file
private string strXMLFile =
private void mitemWriteXml_Click(object sender,
Figure 4. The XML Generated by Executing the WriteXml Method
The XML shown in Figure 6.6
not only contains the data but also reflects the relationship between
the tables; the first category element is the first entry in the file,
and it contains the product elements for all its products, then the next
category element, and within it all its product elements, and so on.
The need to display nested relationships in this manner is the reason
why the DataRelation class has a Nested property.
When we added the FKProdCat data relationship to our data set in the code shown earlier, we added it with a Nested property value of false. To ensure that your XML and XML schema reflect the hierarchical nature of your data set, set the Nested property of your data relationships to true, as shown here:
// Make each relationship a nested relationship
foreach( DataRelation drelForXML in dsetDB.Relations )
drelForXML.Nested = true;
Or, for a single data relationship, use the following code:
// Make the FKProdCat relationship nested.
dsetDB.Relations["FKProdCat"].Nested = true;
It is also possible to
obtain nested XML by having matching primary keys and foreign keys
defined on the respective data tables, but nested relationships are
usually easier to work with because all the information about the
relationship is contained within a single DataRelation object rather than being spread across two constraint objects.
Thus, .NET Compact Framework
data sets give you a convenient way to convert relational data to and
from XML format. This, in turn, gives you a way to save data set data
that your application has captured to a file without incurring the
overhead of using a database to do so.