XML and ADO.NET


As stated in the introduction, XML support is one of the major design goals of ADO.NET and is also central to ADO.NET's internal implementation. Therefore, it makes sense that ADO.NET would have lots of support for XML built into its object model. XML was introduced in Chapter 23, and you are now going to learn about the support for it in ADO.NET.

XML Support in ADO.NET DataSets

The XML support in ADO.NET is centered around the DataSet object, because XML is all about relationships and hierarchically structured data. the DataSet has several methods that process XML, and one of the easiest to use is WriteXml(), which writes out the contents of the DataSet as an XML document.

To use WriteXml(), simply construct a DataSet from existing data using the same code as in the previous examples; use the Fill() method of a data adapter to load the data, define DataRelation objects for the relationships, and so on. Then, simply call WriteXml() on the DataSet you have constructed:

 thisDataSet.WriteXml("nwinddata.xml"); 

WriteXml() can write to various targets; this version of the method simply writes the XML to a file. An external program that accepts XML as an input format can easily read and process the XML.

A ReadXml() method is available also to read the contents of an XML file into a DataSet.

This example takes the code from the DataRelationExample and simply writes out the data in the DataSet to an XML file — the nested foreach loops are simply replaced by the single call to WriteXml(). You will need to ensure that you have a directory named C:\tmp before running this program.

Try It Out – Writing XML from a DataSet

image from book

Follow these steps to modify the DataRelationExample program to write XML:

  1. Open the DataRelationExample project, and replace the foreach loop

    // Print out nested customers and their order IDs foreach (DataRow custRow in thisDataSet.Tables["Customers"].Rows) {    ... }

    with the following code:

     custOrderRel.Nested = true; thisDataSet.WriteXml(@"c:\tmp\nwinddata.xml"); Console.WriteLine( @"Successfully wrote XML output to file c:\tmp\nwinddata.xml"); 

  2. Open Internet Explorer, and browse the C:\tmp\nwinddata.xml file, as shown in Figure 24-14.

    image from book
    Figure 24-14

How It Works

The Nested property of the DataRelation objects tells the WriteXml() method to nest the order details and orders underneath each parent customer in the XML output. The file nwinddata.xml contains all the data in your tables (including all the columns since you specified SELECT * FROM when filling the DataSet). It is in human-readable, easy-to-parse XML format, and the file can be browsed directly in Microsoft Internet Explorer.

So, the DataSet has a WriteXml() — guess what, it also has a ReadXml() method! the ReadXml() method creates and populates a DataTable in a DataSet with the data from an XML file. Furthermore, the DataTable created is given the name of the root element in the XML document.

Having just created an XML file in the previous example, in the next Try It Out you read it back in and display it!

image from book

Try It Out – Reading XML into a DataSet

image from book

Follow these steps to create the ReadingXML example in Visual Studio 2005:

  1. Create a new console application called ReadingXML in the directory C:\BegVCSharp\ Chapter24.

  2. Add a using directive for the System.Data namespace to the top of the code:

     using System.Data; 
  3. Add the following code to the Main() method:

    static void Main(string[] args) { DataSet thisDataSet = new DataSet(); thisDataSet.ReadXml(@"c:\tmp\nwinddata.xml"); foreach (DataRow custRow in thisDataSet.Tables["Customers"].Rows) { Console.WriteLine("Customer ID: " + custRow["CustomerID"] +  " Name: " + custRow["CompanyName"]); } Console.WriteLine("Table created by ReadXml is called {0}", thisDataSet.Tables[0].TableName); Console.Write("Program finished, press Enter/Return to continue:"); Console.ReadLine(); }

  4. Execute the application, and you should see output like that shown in Figure 24-15, provided that you ran the previous example to create the C:\tmp\nwinddata.xml file.

    image from book
    Figure 24-15

How It Works

Note that you are only using one data namespace here — System.Data. You aren't using any database access so you don't have any need for the System.Data.SqlClient or System.Data.OleDb. All you do is create a new DataSet and then use the ReadXml() method to load the data from the C:\tmp\ nwinddata.xml file. The overload of ReadXml() that you use here simply requires you to specify the name of the XML file:

DataSet thisDataSet = new DataSet(); thisDataSet.ReadXml(@"c:\tmp\nwinddata.xml");

Next, you output the contents of the Customers table — this code should be familiar from the DataRelationExample code — you loop through each DataRow in the Rows collection of the Customers table, and display the value of the CustomerID and CompanyName columns:

foreach (DataRow custRow in thisDataSet.Tables["Customers"].Rows) {    Console.WriteLine("Customer ID: " + custRow["CustomerID"] +                       " Name: " + custRow["CompanyName"]); }

How did you know the table was called Customers? As mentioned earlier, the DataTable is named from the root node of the XML document that is read in — if you look back at the screenshot for the WriteXml() method, you will see that the root node of the XML document produced is indeed Customers. Just to prove the point, write out the name of the first DataTable in the Tables collection of the DataSet, using the TableName property of the DataTable:

Console.WriteLine("Table created by ReadXml is called {0}",                    thisDataSet.Tables[0].TableName);
image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net