FunctionX Practical Learning Logo

Save Records of a Table as XML

 

Overview

When using a database and performing data entry, records are usually automatically saved as the user moves on, although this functionality must be implemented by the database developer. If you want, you can save the records of a table in an XML file. This is easily done by calling the DataSet::WriteXml() method.

Practical LearningPractical Learning: Creating a Data Source

  1. Start Microsoft Visual Studio .Net and create a new Windows Forms Application named ExerciseXML1
  2. From the Server Explorer, expand the Servers node, down to the pubs database
  3. Drag the stores table and drop it on the form
  4. Right- click the sqlDataAdapter1 icon in the lower section of the form and click Generate Dataset...
  5. Click the text box to the right of the New radio button and change it to dsStores
     
    Generate Dataset
  6. Click OK
  7. On the Windows Forms section of the Toolbox, click DataGrid and click the form
  8. Position the DataGrid to the top-left corner and enlarge it
  9. Right-click the DataGrid and click Auto Format
  10. In the Formats list, click Professional 1
     
    Auto Format
  11. Click OK
  12. On the form, click the DataGrid to make it is selected. In the Properties windows, change the following properties:
    CaptionText: Book Publishing - Stores
    DataSource: dsStores1.stores
    Anchor: Top, Bottom, Left, Right
  13. Add a button to the bottom-left section of the form. Set its Name to btnLoad and its Caption to Load
  14. Add another button to the bottom-middle section of the form. Set its Name to btnSaveAsXML and its Caption to Save As XML
  15. Add a button to the bottom-right section of the form. Set its Name to btnClose and its Caption to Close
  16. Set the Anchor of all three buttons to Bottom only
     
    Save As XML
  17. Double-click the Load, the Save As XML, and the Close buttons to generate their events
  18. Implement them as follows:
     
    System::Void btnLoad_Click(System::Object *  sender, 
    				System::EventArgs *  e)
    {
    	 this->sqlDataAdapter1->Fill(this->dsStores1);
    }
    
    System::Void btnSaveAsXML_Click(System::Object *  sender, 
    					System::EventArgs *  e)
    {
    	 this->dsStores1->WriteXml(S"stores.xml");
    }
    
    System::Void btnClose_Click(System::Object *  sender, 
    				System::EventArgs *  e)
    {
    	 Close();
    }
  19. Press Ctrl + F5 to execute the application and accept to save it:
     
    Stores
  20. Click Load
  21. When the records are displaying, click Save As XML
  22. After viewing the form, close it
  23. Locate the folder where the project was created and double-click the stores.xml file

 

Home Copyright © 2004 FunctionX, Inc.