Udostępnij za pośrednictwem


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:

  1. Creating a new project.

  2. Creating an XML file to be read into the dataset.

  3. Creating the user interface.

  4. Creating the dataset, read the XML file, and display it in a DataGridView control.

  5. Adding code to display the XML Schema based on the XML file in a TextBox control.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

Create a New Project

In this step, you will create a Visual Basic, Visual C#, or Visual J# project that will contain this walkthrough.

To create the new Windows project

  1. From the File menu, create a new project.

  2. Name the project ReadingXML.

  3. Select Windows Application and click OK. For more information, see Creating Windows-Based Applications.

    The ReadingXML project is created and added to Solution Explorer.

Generate the XML File to Be Read into the Dataset

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

  1. From the Project menu, choose Add New Item.

  2. Select XML File, name the file authors.xml, and click Add.

    The XML file loads into the designer and is ready for edit.

  3. 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>
    
  4. From the File menu, point to Save authors.xml.

Create the User Interface

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

  1. Open Form1 in design view.

  2. From the Toolbox, drag the following controls onto the form:

  3. Set the following properties:

    Control

    Property

    Setting

    TextBox1

    Multiline

    true

     

    ScrollBars

    Vertical

    Button1

    Name

    ReadXmlButton

     

    Text

    Read XML

    Button2

    Name

    ShowSchemaButton

     

    Text

    Show Schema

Create the Dataset that will Receive the XML Data

In this next procedure, you create a new dataset named authors. For more information about datasets, see Datasets in Visual Studio Overview.

To create a new dataset that will receive the XML data

  1. With the source file for Form1 selected in Solution Explorer, click the View Designer button in the Solution Explorer toolbar.

  2. From the Data Tab, Toolbox, drag a DataSet onto Form1.

  3. Select Untyped dataset on the Add Dataset Dialog Box, and then click OK.

    DataSet1 is added to the component tray.

  4. In the Properties window, set the Name and DataSetName properties to AuthorsDataSet.

Create the Event Handler to Read the XML into the Dataset

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

  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 ReadXmlButton_Click event handler.

  3. Type the following code into the ReadXmlButton_Click event handler:

    Private Sub ReadXmlButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles ReadXmlButton.Click
    
        Dim filePath As String = "Complete path where you saved the XML file"
    
        AuthorsDataSet.ReadXml(filePath)
    
        DataGridView1.DataSource = AuthorsDataSet
        DataGridView1.DataMember = "authors" 
    End Sub
    
    private void ReadXmlButton_Click(object sender, EventArgs e)
    {
        string filePath = "Complete path where you saved the XML file";
    
        AuthorsDataSet.ReadXml(filePath);
    
        dataGridView1.DataSource = AuthorsDataSet;
        dataGridView1.DataMember = "authors";
    }
    
  4. In the ReadXMLButton_Click event handler code, change the filepath = entry to the correct path.

Create the Event Handler to Display the Schema in the Textbox

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

  1. In Solution Explorer, select Form1 and click the View Designer button.

  2. Double-click the Show Schema button.

    The Code Editor opens at the ShowSchemaButton_Click event handler.

  3. Type the following code into the ShowSchemaButton_Click event handler.

    Private Sub ShowSchemaButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles ShowSchemaButton.Click
    
        Dim swXML As New System.IO.StringWriter()
        AuthorsDataSet.WriteXmlSchema(swXML)
        TextBox1.Text = swXML.ToString
    End Sub
    
    private void ShowSchemaButton_Click(object sender, EventArgs e)
    {
        System.IO.StringWriter swXML = new System.IO.StringWriter();
        AuthorsDataSet.WriteXmlSchema(swXML);
        textBox1.Text = swXML.ToString();
    }
    

Testing

You can now test the form to make sure it behaves as expected.

To test the form

  1. Press F5 to run the application.

  2. Click the Read XML button.

    The DataGridView displays the contents of the XML file.

  3. Click the Show Schema button.

    The text box displays the XML Schema for the XML file.

Next Steps

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.

See Also

Other Resources

Data Walkthroughs

Accessing Data (Visual Studio)

Preparing Your Application to Receive Data

XML Tools in Visual Studio