programming4us
programming4us
DATABASE

.NET Compact Framework 3.5 : Working with Data Sets (part 3) - Reading and Writing a Data Set as XML

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
6/30/2012 3:14:51 PM

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:

  1. WriteXml: writes the contents of a data set as XML to a file

  2. WriteXmlSchema: writes the structure of the data set as an XML schema to a file

  3. ReadXml: reads the contents of an XML file into a data set

  4. 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.
dsetDB.Relations.Add(
   "FKProdCat",
   dsetDB.Tables["Categories"].Columns["CategoryID"],
   dsetDB.Tables["Products"].Columns["CategoryID"],
   true);

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 =
                        @"My Documents\ourProduceCo.xml";
         :
         :
private void mitemWriteXml_Click(object sender,
                                 EventArgs e)
{
   dsetDB.WriteXml(strXMLFile);
}

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.

Other  
  •  .NET Compact Framework 3.5 : Examining ADO.NET
  •  Using SQL Server 2005 Integration Services : Programming Integration Services (part 4) - Connecting the Source and Destination Adapters with a Path
  •  Using SQL Server 2005 Integration Services : Programming Integration Services (part 3) - Setting Up Column Information
  •  Using SQL Server 2005 Integration Services : Programming Integration Services (part 2)
  •  Using SQL Server 2005 Integration Services : Programming Integration Services (part 1) - Creating Packages Programmatically - Data Flow
  •  Using SQL Server 2005 Integration Services : Working with Integration Services Packages (part 2) - Data Flow
  •  Using SQL Server 2005 Integration Services : Working with Integration Services Packages (part 1) - Control Flow
  •  SQL Server 2005 : Extending Your Database System with Data Mining - Data Mining Applied (part 2)
  •  SQL Server 2005 : Extending Your Database System with Data Mining - Data Mining Applied (part 1)
  •  # Oracle Coherence 3.5 : Achieving Performance, Scalability, and Availability Objectives (part 2)
  •  
    Top 10
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
    - Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
    - Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
    - Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
    - Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    programming4us programming4us
    programming4us
     
     
    programming4us