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. The application also displays the XML in a TextBox.

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

1 // Fig. 20.50: XMLWriter.cs 2 // Demonstrates generating XML from an ADO.NET DataSet. 3 using System; 4 using System.Windows.Forms; 5 6 namespace XMLWriter 7 { 8 public partial class XMLWriterForm : Form 9 { 10 public XMLWriterForm() 11 { 12 InitializeComponent(); 13 } // end XMLWriterForm constructor 14 15 // Click event handler for the Save Button in the 16 // BindingNavigator saves the changes made to the data 17 private void playersBindingNavigatorSaveItem_Click( 18 object sender, EventArgs e ) 19 { 20 this.Validate(); 21 this.playersBindingSource.EndEdit(); 22 this.playersTableAdapter.Update( this.baseballDataSet.Players ); 23 } // end method bindingNavigatorSaveItem_Click 24 25 // loads data into the baseballDataSet.Players table 26 private void XMLWriterForm_Load( object sender, EventArgs e ) 27 { 28 // TODO: This line of code loads data into the 29 // 'baseballDataSet.Players' table. You can move, 30 // or remove it, as needed. 31 this.playersTableAdapter.Fill( this.baseballDataSet.Players ); 32 } 33 34 // write XML representation of DataSet when Button clicked 35 private void writeButton_Click( object sender, EventArgs e ) 36 { 37 // set the namespace for this DataSet 38 // and the resulting XML document 39 baseballDataSet.Namespace = "http://www.deitel.com/baseball"; 40 41 // write XML representation of DataSet to a file 42 baseballDataSet.WriteXml( "Players.xml" ); 43 44 // display XML representation in TextBox 45 outputTextBox.Text += "Writing the following XML: " + 46 baseballDataSet.GetXml() + " "; 47 } // end method writeButton_Click 48 } // end class XMLWriterForm 49 } // end namespace XMLWriter

We created this GUI by first adding the Baseball.mdf database (located in the chapter's examples directory) to the project, 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 writeButton and the TextBox outputTextBox. 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 outputTextBox's ReadOnly and MultiLine properties to true and its ScrollBars property to Vertical. Create the event handler for writeButton by double clicking it in Design view.

The autogenerated XMLWriterForm_Load event handler (lines 2632) 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 3547 define the event handler for the Write to XML button. When the user clicks this button, line 39 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 42 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 4546 then display this XML representation, obtained by invoking 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 39 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

Категории

© amp.flylib.com,