Data Sets and XML

A DataSet object is not only a collection of tables, each with rows and columns . You can also think of it as an XML document, and work with it as though it were XML. The DataSet class has methods that produce and read XML with little or no work on your part.

Getting XML from a DataSet

To see how quickly you can create XML from a database, add another button to EmployeeUI , and change the text to Write XML and the name to WriteXML . Double-click the button to edit the click handler, and add this code:

 
 private: System::Void WriteXML_Click(System::Object *  sender,                                      System::EventArgs *  e) {     ds->WriteXml("db.xml"); } 

How hard is that? The data set will write itself out to a file as XML; all you need to do is provide a filename.

Build and run the application, enter a search string, click Lookup, and when the results appear, click Write XML. Browse to the project folder for EmployeeUI and you should see that a new file, db.xml, has appeared. Drag this file into Visual Studio to open it and see the XML. For example, entering GR in the text box, the XML generated looks like this:

 
 <?xml version=" 1.0"  encoding=" utf-8" ?> <Employees>   <Employees>     <EmployeeID>250</EmployeeID>     <EmployeeName>Greene, Nancy</EmployeeName>     <EmployeeRate>6.55</EmployeeRate>     <DeptID>SPORTING</DeptID>   </Employees> </Employees> 

You might notice there are two <Employees> tags, one inside the other. The outer one comes from the name of the data set, which was set in Lookup() . The inner one is the name of the table. If the data set held several tables, it would write them all out inside the outer <Employees> tag.



Microsoft Visual C++. NET 2003 Kick Start
Microsoft Visual C++ .NET 2003 Kick Start
ISBN: 0672326000
EAN: 2147483647
Year: 2002
Pages: 141
Authors: Kate Gregory

Similar book on Amazon

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