As I mentioned earlier, the DataSet is invaluable when you need to share data with a non-.NET client. Almost every modern programming language can parse an XML document, making XML an ideal way to exchange shared data. Best of all, the DataSet exports data seamlessly to XML. That means you don't need to write any conversion code. To create a simple test, we'll save the DataSet to a file using its built-in WriteXml method. (You also can save the XSD schema using the WriteXmlSchema method.) Dim DB As New CustomerDB() Dim ds As DataSet = DB.GetCustomers() ds.WriteXml("c:\customers.xml") The default representation uses a DataSet document tag, which contains <Customer> tags for every row in the Customers table. The <Customer> tags contain subtags for each field in an individual row. A portion of this file is shown in Listing 3-20. Listing 3-20 DataSet XML<?xml version="1.0" standalone="yes"?> <NewDataSet> <Customers> <CustomerID>1</CustomerID> <FullName>James Bondwell</FullName> <EmailAddress>jb@ibuyspy.com</EmailAddress> <Password>IBS_007</Password> </Customers> <Customers> <CustomerID>2</CustomerID> <FullName>Sarah Goodpenny</FullName> <EmailAddress>sg@ibuyspy.com</EmailAddress> <Password>IBS_001</Password> </Customers> <Customers> <CustomerID>3</CustomerID> <FullName>Gordon Que</FullName> <EmailAddress>gq@ibuyspy.com</EmailAddress> <Password>IBS_000</Password> </Customers> </NewDataSet> To process this file, we'll use Visual Basic 6, with the help of the Microsoft COM-based MSXML parser (available for download from http://msdn.microsoft.com/xml). The client code simply opens the file and displays an entry in a list control for each customer, using the name and e-mail address, as illustrated in Listing 3-21 and Figure 3-7. Listing 3-21 Displaying the DataSet in Visual Basic 6Private Sub Form_Load() Dim Doc As MSXML2.DOMDocument40 Set Doc = New MSXML2.DOMDocument40 Doc.async = False Doc.Load ("c:\customers.xml") Dim Child As MSXML2.IXMLDOMNode For Each Child In Doc.documentElement.childNodes ' The first node (offset 0) is the ID. ' The second node (offset 1) is the name. ' The third node (offset 2) is the email. lstNames.AddItem (Child.childNodes(1).Text & _ " (" & Child.childNodes(2).Text & ")") Next End Sub Figure 3-7. Reading the DataSet in a Visual Basic 6 client
This code demonstrates how easily DataSet-based information can be interpreted by a non-.NET client, but it doesn't provide a practical way to exchange the data. Typically, you'll want to create a .NET client that communicates directly with an XML Web service rather than one that relies on another component that saves the information to a file. In Chapter 5, I'll explain how to take this extra step. Note Unfortunately, the DataSet doesn't provide many options for tailoring the XML. If you need to support a client that expects different element names or a specific schema, you will have to manually process and modify the DataSet XML with the types in the Systeml.Xml namespace. |