Section 20.9. Using a DataSet to Read and Write XML


20.9. Using a DataSet to Read and Write XML

A powerful feature of ADO.NET is its ability to convert data stored in a data source to XML for exchanging data between applications in a portable format. Class DataSet of namespace System.Data provides methods WriteXml, ReadXml and GetXml, which enable developers to create XML documents from data sources and to convert data from XML into data sources.

Writing Data from a Data Source to an XML Document

The application of Fig. 20.50 populates a DataSet with statistics about baseball players, then writes the data to an XML document and displays the XML in a TextBox.

Figure 20.50. Writing the XML representation of a DataSet to a file.

  1   ' Fig. 20.50: FrmXMLWriter.vb  2   ' Demonstrates generating XML from an ADO.NET DataSet.  3   Public Class FrmXMLWriter  4      ' Click event handler for the Save Button in the  5      ' BindingNavigator saves the changes made to the data  6      Private Sub  PlayersBindingNavigatorSaveItem_Click( _  7         ByVal sender As  System.Object, ByVal  e As  System.EventArgs) _  8         Handles  PlayersBindingNavigatorSaveItem.Click  9 10         Me.Validate() 11         Me.PlayersBindingSource.EndEdit() 12         Me .PlayersTableAdapter.Update(Me.BaseballDataSet.Players) 13      End Sub  ' PlayersBindingNavigatorSaveItem_Click 14 15      ' loads data into the BaseballDataSet.Players table 16      Private Sub  FrmXMLWriter_Load(ByVal  sender As   System.Object, _ 17         ByVal  e As  System.EventArgs) Handles MyBase .Load 18         ' TODO: This line of code loads data into the 19         ' 'BaseballDataSet.Players' table. You can  move, or remove it, 20         ' as needed. 21         Me.PlayersTableAdapter.Fill(Me .BaseballDataSet.Players) 22      End Sub  ' FrmXMLWriter_Load 23 24      ' write XML representation of DataSet when Button clicked 25      Private Sub  btnWrite_Click(ByVal  sender As  System.Object, _ 26         ByVal  e As   System.EventArgs) Handles  btnWrite.Click 27         ' set the namespace for this DataSet                         28         ' and the resulting XML document                             29         BaseballDataSet.Namespace = "http://www.deitel.com/baseball" 30 31         ' write XML representation of DataSet to a file 32         BaseballDataSet.WriteXml("Players.xml")        33 34         ' display XML representation in TextBox 35         txtOutput.Text = "Writing the following XML:" & vbCrLf & _ 36            BaseballDataSet.GetXml()  + vbCrLf 37     End Sub  ' btnWrite_Click 38   End Class  ' FrmXMLWriter 

We created this GUI by adding the Baseball.mdf database (located in the chapter's examples directory) as a data source, then dragging the Players node from the Data Sources window to the Form. This action created the BindingNavigator and DataGridView seen in the output of Fig. 20.50. We then added the Button btnWrite and the TextBox txtOutput. The XML representation of the Players table should not be edited and will span more lines than the TextBox can display at once, so we set txtOutput's ReadOnly and MultiLine properties to TRue and its ScrollBars property to Vertical. Create the event handler for btnWrite by double clicking it in Design view.

The autogenerated FrmXMLWriter_Load event handler (lines 1622) calls method Fill of class PlayersTableAdapter to populate BaseballDataSet with data from the Players table in the Baseball database. Note that the IDE binds the DataGridView to BaseballDataSet.Players (through the PlayersBindingSource) to display the information to the user.

Lines 2537 define the event handler for the Write to XML button. When the user clicks this button, line 29 sets BaseballDataSet's Namespace property to specify a namespace for the DataSet and any XML documents based on the DataSet (see Section 19.4 to learn about XML namespaces). Line 32 invokes DataSet method WriteXml, which generates an XML representation of the data contained in the DataSet, then writes the XML to the specified file. This file is created in the project's bin/Debug or bin/Release directory, depending on how you executed the program. Lines 3536 then display this XML representation. Line 36 invokes DataSet method GetXml, which returns a String containing the XML.

Examining an XML Document Generated By DataSet Method WriteXml

Figure 20.51 presents the Players.xml document generated by DataSet method WriteXml in Fig. 20.50. Note that the BaseballDataSet root element (line 2) declares the document's default namespace to be the namespace specified in line 29 of Fig. 20.50. Each Players element represents a record in the Players table. The PlayerID, FirstName, LastName and BattingAverage elements correspond to the columns with these names in the Players database table.

Figure 20.51. XML document generated from BaseballDataSet in XMLWriter.

  1   <?xml version ="1.0"  standalone ="yes"?>  2   <BaseballDataSet xmlns ="http://www.deitel.com/baseball">  3     <Players>                                 4       <PlayerID> 1 </PlayerID>                5       <FirstName> John </FirstName>           6       <LastName> Doe </LastName>              7       <BattingAverage> 0.375 </BattingAverage>  8     </Players>                                  9     <Players> 10       <PlayerID> 2  </PlayerID> 11       <FirstName> Jack </FirstName> 12       <LastName> Smith </LastName> 13       <BattingAverage> 0.223 </BattingAverage> 14     </Players> 15     <Players> 16      <PlayerID> 3  </PlayerID> 17      <FirstName> George  </FirstName> 18       <LastName> O'Malley  </LastName> 19       <BattingAverage> 0.344  </BattingAverage> 20     </Players> 21   </BaseballDataSet> 



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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