DATABASE

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

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)
  •  # Oracle Coherence 3.5 : Achieving Performance, Scalability, and Availability Objectives (part 1)
  •  MySQL Server Monitoring (part 3) - Server Logs, Third-Party Tools & The MySQL Benchmark Suite
  •  MySQL Server Monitoring (part 2) - MySQL Administrator
  •  MySQL Server Monitoring (part 1) - SQL Commands
  •  Using MySQL Enterprise (part 3) - Query Analyzer
  •  Using MySQL Enterprise (part 2) - Monitoring
  •  Using MySQL Enterprise (part 1) - Installation & Fixing Monitoring Agent Problems
  •  Getting Started with MySQL Enterprise & MySQL Enterprise Components
  •  Transact-SQL in SQL Server 2008 : Table-Valued Parameters
  •  Transact-SQL in SQL Server 2008 : New date and time Data Types and Functions
  •  
    Top 10
    SharePoint 2010 : The Search User Interface - The Search Center
    SharePoint 2010 : The Search User Interface - The Query Box
    SQL Server 2008 R2 : Database Maintenance - Executing a Maintenance Plan
    SQL Server 2008 R2 : Database Maintenance - Managing Maintenance Plans Without the Wizard
    Game Programming with DirectX : 3D Models - OBJ Models (part 3) - Preparing OBJ Files for Direct3D
    Game Programming with DirectX : 3D Models - OBJ Models (part 2) - Loading OBJ Files
    Game Programming with DirectX : 3D Models - OBJ Models (part 1) - Understanding the OBJ Model Format
    Game Programming with DirectX : 3D Models - Token Stream
    Game Programming with DirectX : 3D Models - Files in C++
    A Look At Truecrypt The Open Source Security Tool
    Most View
    Safe online transactions (Part 1) - Shared keys & Public key cryptography
    Buyer’s Guide - Keyboard and mice (Part 2) - Gigabyte Multimedia Ultra-slim Profile Keyboard GK-K6150, Microsoft Natural Ergonomic Keyboard 40000
    Surface Pro - More Than A Tablet
    Programming .NET Components : Building a Distributed Application (part 5) - Creating Remote Objects
    Programming the Mobile Web : Mobile Rich Internet Applications (part 2) - JavaScript Mobile UI Patterns
    System Center Configuration Manager 2007 : Architecture Design Planning - Software Update Planning
    Ultrabook Supertest (Part 8)
    Great Business Apps for Your iPad/iPod/iPhone : Car Butler, List n Do, Magnifying Glass with Light, WorkSnug
    User Account Control in Windows Vista: Basic
    Top 10 Laptops - Q1 2013
    Intel’s Next Unit Of Computing Review - The Future Desktop Or Another Nettop? (Part 2)
    300Mbps AV200 Wireless N Powerline Extender TL-WPA281
    Registering CLR Assemblies for Use with SQL Server
    HP ProLiant Servers AIS : Server Chipsets (part 2) - ProFusion Chipset, F8 Chipset
    How Not To Build A PC (Part 3)
    Windows 8: End Game? (Part 2)
    Can Software Be Patented?
    The Best Entry Level Phones – November 2012 (Part 1) - Sony Xperia Miro
    Diving Deeper With Suunto
    What To Do With An Old Mac (Part 1)