A Word About XML

A Word About XML

One of ADO.NET s most touted features is its seamless support for XML. But what does seamless XML support really mean? In answer to that question, check out the following code, which calls ReadXml on a DataSet to read an XML file from disk:

DataSet ds = new DataSet (); ds.ReadXml ("Rates.xml");

Chapter 5 s Converter program used code remarkably similar to this to read an XML file and populate a ListBox control by iterating over the rows in the resulting DataTable and calling Add on the control s Items collection:

foreach (DataRow row in ds.Tables[0].Rows) Currencies.Items.Add (row["Currency"].ToString ());

The Currency elements in the XML file metamorphosed into a Currency column in the DataTable, and Exchange elements representing currency exchange rates became an Exchange column.

DataSet.ReadXml is a powerful method that renders a DataSet equally capable of handling relational data and XML data. Reading an XML file into a DataSet transforms XML data into relational data and vastly simplifies the handling of XML. Once the data is in the DataSet, you can perform queries on it using DataTable.Select and even write it to a database using a DataAdapter. How might that come in handy? Suppose someone sends your company an invoice as an XML file and you want to process the invoice. Reading the XML into a DataSet simplifies the process of parsing the data and of storing it permanently in a database along with other records that your company keeps. That s worlds easier than using MSXML or other XML parsers to manually iterate over the data and write it to a database with SQL commands. Nor is ReadXml limited to working exclusively with files. It s equally capable of reading from streams and readers.

ReadXml is complemented by a DataSet method named WriteXml. When I need to create XML documents, I often do so by building a DataSet and calling WriteXml on it. WriteXml is especially convenient for converting relational data into XML. Using a DataAdapter to initialize a DataSet with a database query and writing the results to an XML file with WriteXml makes relational-to-XML data conversions an absolute breeze.

That s seamless integration. XML is a language for machines, not humans, but developers have expended untold hours in recent years using XML parsers to read XML data and XML writers to write XML. With the advent of the .NET Framework, XML becomes light-years easier to deal with in part because of the high level of support for it in DataSet and in part thanks to the XML classes in the FCL.

Speaking of XML classes in the FCL: what about them? Shouldn t a book on the .NET Framework describe those classes somewhere? Turn the page and you ll find out.



Programming Microsoft  .NET
Applied MicrosoftNET Framework Programming in Microsoft Visual BasicNET
ISBN: B000MUD834
EAN: N/A
Year: 2002
Pages: 101

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