Read XML data into a dataset

 

For the latest documentation on Visual Studio 2017, see Read XML data into a dataset on docs.microsoft.com. ADO.NET provides simple methods for working with XML data. In this walkthrough, you create a Windows application that loads XML data into a dataset. The dataset is then displayed in a DataGridView control. Finally, an XML schema based on the contents of the XML file is 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, reading the XML file, and displaying it in a DataGridView control

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

System_CAPS_ICON_note.jpg Note

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

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

To create the new Windows project

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

  2. Name the project ReadingXML.

  3. Select Windows Application, and then selectOK. For more information, see Client 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

  1. On the Project menu, selectAdd New Item.

  2. Select XML File, name the file authors.xml, and then selectAdd.

    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. On the File menu, selectSave authors.xml.

The user interface for this application consists of the following:

  • A DataGridView control that displays the contents of the XML file as data.

  • A TextBox control that displays 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 T:System.IO.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:

    ControlPropertySetting
    TextBox1Multilinetrue
    ScrollBarsVertical
    Button1NameReadXmlButton
    TextRead XML
    Button2NameShowSchemaButton
    TextShow Schema

In this step, you create a new dataset named authors. For more information about datasets, see Dataset tools in Visual Studio.

To create a new dataset that receives the XML data

  1. In Solution Explorer, select the source file for Form1, and then select the View Designer button on the Solution Explorer toolbar.

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

  3. In the Add Dataset dialog box, select Untyped dataset, and then selectOK.

    DataSet1 is added to the component tray.

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

The Read XML button reads the XML file into the dataset. It then 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 then select the View Designer button on the Solution Explorer toolbar.

  2. Select 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 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.

The Show Schema button creates a StringWriter object that's filled with the schema and is displayed in the TextBoxcontrol.

To add code to the ShowSchemaButton_Click event handler

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

  2. Select 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 void ShowSchemaButton_Click(object sender, EventArgs e)
            {
                System.IO.StringWriter swXML = new System.IO.StringWriter();
                AuthorsDataSet.WriteXmlSchema(swXML);
                textBox1.Text = swXML.ToString();
            }
    

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

To test the form

  1. SelectF5 to run the application.

  2. Select the Read XML button.

    The DataGridView displays the contents of the XML file.

  3. Select the Show Schema button.

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

This walkthrough teaches you 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 you might do 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.

Data Walkthroughs
Accessing data in Visual Studio
Preparing Your Application to Receive Data
XML Tools in Visual Studio

Show: