One of ADO.NET鈥檚 most touted features is its seamless support for XML. But what does 鈥渟eamless 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:
Chapter 5鈥檚 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鈥檚 Items collection:
The Currency elements in the XML file metamorphosed into a 鈥淐urrency鈥?column in the DataTable, and Exchange elements representing currency exchange rates became an 鈥淓xchange鈥?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鈥檚 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鈥檚 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鈥檚 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鈥檛 a book on the .NET Framework describe those classes somewhere? Turn the page and you鈥檒l find out.