Walkthrough: Reading XML Data into a Dataset
ADO.NET provides simple methods for working with XML data. In this walkthrough you will create a Windows application that will load XML data into a dataset. The dataset will then be displayed in a DataGridView. Finally, an XML Schema based on the contents of the XML file will be displayed in a text box.
This walkthrough consists of five main steps:
Creating a new project.
Creating an XML file to be read into the dataset.
Creating the user interface.
Creating the dataset, read the XML file, and display it in a DataGridView control.
Adding code to display the XML Schema based on the XML file in a TextBox control.
Note |
|---|
|
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings. |
In this step, you will create a Visual Basic or Visual C# project that will contain this walkthrough.
To create the new Windows project
-
From the File menu, create a new project.
-
Name the project ReadingXML.
-
Select Windows Application and click OK. For more information, see Creating Windows-Based Applications.
The ReadingXML project is created and added to Solution Explorer.
Because this walkthrough focuses on reading XML data into a dataset, the contents of an XML file is provided.
To create the XML file that will be read into the dataset
-
From the Project menu, choose Add New Item.
-
Select XML File, name the file authors.xml, and click Add.
The XML file loads into the designer and is ready for edit.
-
Paste the following code into the editor below the XML declaration:
<Authors_Table> <authors> <au_id>172-32-1176</au_id> <au_lname>White</au_lname> <au_fname>Johnson</au_fname> <phone>408 496-7223</phone> <address>10932 Bigge Rd.</address> <city>Menlo Park</city> <state>CA</state> <zip>94025</zip> <contract>true</contract> </authors> <authors> <au_id>213-46-8915</au_id> <au_lname>Green</au_lname> <au_fname>Margie</au_fname> <phone>415 986-7020</phone> <address>309 63rd St. #411</address> <city>Oakland</city> <state>CA</state> <zip>94618</zip> <contract>true</contract> </authors> <authors> <au_id>238-95-7766</au_id> <au_lname>Carson</au_lname> <au_fname>Cheryl</au_fname> <phone>415 548-7723</phone> <address>589 Darwin Ln.</address> <city>Berkeley</city> <state>CA</state> <zip>94705</zip> <contract>true</contract> </authors> <authors> <au_id>267-41-2394</au_id> <au_lname>Hunter</au_lname> <au_fname>Anne</au_fname> <phone>408 286-2428</phone> <address>22 Cleveland Av. #14</address> <city>San Jose</city> <state>CA</state> <zip>95128</zip> <contract>true</contract> </authors> <authors> <au_id>274-80-9391</au_id> <au_lname>Straight</au_lname> <au_fname>Dean</au_fname> <phone>415 834-2919</phone> <address>5420 College Av.</address> <city>Oakland</city> <state>CA</state> <zip>94609</zip> <contract>true</contract> </authors> </Authors_Table> -
From the File menu, point to Save authors.xml.
The user interface for this application will consist of the following:
-
A DataGridView control that will display the contents of the XML file as data.
-
A TextBox control that will display the XML Schema for the XML file.
-
Two Button controls.
-
One button reads the XML file into the dataset and displays it in the DataGridView control.
-
A second button extracts the schema from the dataset, and through a StringWriter displays it in the TextBox control.
-
To add controls to the form
-
Open Form1 in design view.
-
From the Toolbox, drag the following controls onto the form:
-
One DataGridView control
-
One TextBox control
-
Two Button controls
-
-
Set the following properties:
Control
Property
Setting
TextBox1
Multiline
true
ScrollBars
Vertical
Button1
Name
ReadXmlButton
Text
Read XML
Button2
Name
ShowSchemaButton
Text
Show Schema
In this next procedure, you create a new dataset named authors. For more information about datasets, see Working with Datasets in Visual Studio.
To create a new dataset that will receive the XML data
-
With the source file for Form1 selected in Solution Explorer, click the View Designer button in the Solution Explorer toolbar.
-
From the Data Tab, Toolbox, drag a DataSet onto Form1.
-
Select Untyped dataset on the Add Dataset Dialog Box, and then click OK.
DataSet1 is added to the component tray.
-
In the Properties window, set the Name and DataSetName properties to AuthorsDataSet.
The Read XML button reads the XML file into the dataset and sets properties on the DataGridView control that bind it to the dataset.
To add code to the ReadXmlButton_Click event handler
-
In Solution Explorer, select Form1 and click the View Designer button on the Solution Explorer toolbar.
-
Double-click the Read XML button.
The Code Editor opens at the ReadXmlButton_Click event handler.
-
Type the following code into the ReadXmlButton_Click event handler:
-
In the ReadXMLButton_Click event handler code, change the filepath = entry to the correct path.
The Show Schema button creates a StringWriter object that is filled with the schema and displayed in the TextBox .
To add code to the ShowSchemaButton_Click event handler
-
In Solution Explorer, select Form1 and click the View Designer button.
-
Double-click the Show Schema button.
The Code Editor opens at the ShowSchemaButton_Click event handler.
-
Type the following code into the ShowSchemaButton_Click event handler.
This walkthrough shows the basics of reading an XML file into a dataset, as well as creating a schema based on the contents of the XML file. Here are some tasks that might come next:
-
Edit the data in the dataset and write it back out as XML. For more information, see WriteXml.
-
Edit the data in the dataset and write it out to a database. For more information, see Saving Data.
Note