See Also
Data Walkthroughs | Introduction to Datasets | DataSet.ReadXml Method |
DataSet.WriteXmlSchema Method | Debugging Managed Code | Deploying Applications |
XML in Visual Studio
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 DataGrid control. Finally, an XML Schema based on the contents of the XML file will be displayed in a text box.
This walkthrough will consist of five main steps:
1.
Create a new Visual Basic or Visual C# project.
2.
Create an XML file to be read into the dataset.
3.
Create the user interface.
4.
Add code to create the dataset, read the XML file, and display it in a DataGrid control.
5.
Add code to display the XML Schema based on the XML file in a TextBox control.
In this step, you will create a Visual Basic or Visual C# project that will contain this walkthrough.
To create a new project
1.
From the File menu, point to New, and then click Project to display the New
Project dialog box.
2.
Depending on what language you want to use, select Visual Basic Projects or
Visual C# Projects in the Project Types pane, and then select Windows
Application.
3.
Name the project ReadingXML, then click OK.
Visual Studio will add the ReadingXML project to Solution Explorer and display a new Windows Form in the designer.
Because this walkthrough focuses on reading XML data into a dataset, the contents of an
XML file is provided. This XML file must be located in a folder that will be accessible from the machine that you are using to create this application.
To create the XML file that will be read into the dataset
1.
From the File menu, point to New, and then click File.
The New File dialog box appears.
2.
Select XML File and click Open.
The XML file loads into the designer and is ready for edit.
3.
Paste the following code into the editor below the XML declaration:
Tip Right-click the editor and select Paste as HTML.
<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>
4.
From the File menu, point to Save XMLFile1 As.
The Save File As dialog box appears.
5.
Name the file authors.xml and click Save.
Note Be sure to remember the complete path, it will be needed later in this walkthrough.
The user interface for this application will consist of the following:
A DataGrid 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
DataGrid control.
A second button extracts the schema from the dataset, and through a
StringWriter displays it in the TextBox control.
Initial layout for this application
To add controls to the form
1.
From the Windows Forms tab of the Toolbox, drag the following controls onto the form:
1 DataGrid control
1 TextBox control
2 Button controls
2.
Set the following properties:
Control Property
TextBox1 Multiline
ScrollBars
Button1
Button2
Name
Text
Name
Text
Setting true
Vertical btnReadXML
Read XML btnShowSchema
Show Schema
You will programmatically create a new dataset named authors. It will be declared at form level in order to be accessible from the button_click event handlers. For more information about datasets, see Introduction to Datasets .
To add the code to create a new dataset that will receive the XML data
1.
With Form1.vb (VB users) or Form1.cs (C# users) selected in Solution Explorer, click the View Code button in the Solution Explorer toolbar.
2.
In the declarations area, declare a new dataset named authors:
' Visual Basic
Dim dsAuthors As New DataSet("authors")
// C#
DataSet dsAuthors = new DataSet("authors");
The Read XML button reads the XML file into the dataset and sets properties on the
DataGrid that bind it to the dataset.
To add code to the btnReadXML_Click event handler
1.
In Solution Explorer, select Form1 and click the View Designer button on the
Solution Explorer toolbar.
2.
Double-click the Read XML button.
The Code Editor opens at the btnReadXML_Click event handler.
3.
Type the following code into the btnReadXML_Click event handler:
4.
' Visual Basic
Dim filePath As String filePath = " Complete path where you saved the XML file" dsAuthors.ReadXml(filePath)
With DataGrid1
.DataSource = dsAuthors
.DataMember = "authors"
.CaptionText = .DataMember
End With
// C# string filePath = "Complete path where you saved the XML file"; dsAuthors.ReadXml(filePath);
dataGrid1.DataSource = dsAuthors; dataGrid1.DataMember = "authors"; dataGrid1.CaptionText = dataGrid1.DataMember;
The Show Schema button creates a StringWriter object that is filled with the schema and displayed in the TextBox.
To add code to the btnShowSchema_Click event handler
1.
In Solution Explorer, select Form1.vb and click the View Designer button.
2.
Double-click the Show Schema button.
The Code Editor opens at the btnShowSchema_Click event handler.
3.
Type the following code into the btnShowSchema_Click event handler.
' Visual Basic
Dim swXML As New System.IO.StringWriter() dsAuthors.WriteXmlSchema(swXML)
TextBox1.Text = swXML.ToString
// C#
System.IO.StringWriter swXML = new System.IO.StringWriter(); dsAuthors.WriteXmlSchema(swXML); textBox1.Text = swXML.ToString();
1.
Press F5 to run the application.
2.
Click the Read XML button.
The data grid displays the contents of the XML file.
3.
Click the Show Schema button.
The text box displays the XML Schema for the XML file.
Result of running the application
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 DataSet.WriteXml Method.
Edit the data in the dataset and write it out to a database. For more information, see
Updating a Data Source with a Dataset .
See Also
Data Walkthroughs | Introduction to Datasets | DataSet.ReadXml Method |
DataSet.WriteXmlSchema Method | Debugging Managed Code | Deploying Applications |
XML in Visual Studio
Send feedback to Microsoft
© 2001 Microsoft Corporation. All rights reserved.