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;
 6 namespace XMLWriter
 7 {
 8 public partial class XMLWriterForm : Form
 9 {
10 public XMLWriterForm()
11 {
12 InitializeComponent();
13 } // end XMLWriterForm constructor
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
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 = ""; 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 "1.0" standalone="yes"?>
 2  xmlns="">
 4  <PlayerID>1 
 5  John 
 6  Doe 
 7  0.375
10 2
11 Jack
12 Smith
13 0.223
16 3
17 George
18 O'Malley
19 0.344



    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look


    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2


    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures



    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types


    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600 © 2008-2020.
    If you may any questions please contact us: