programming4us
programming4us
DATABASE

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us