Muokkaa

Jaa


Read XML data into a dataset in .NET Framework applications

Note

Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. The technologies are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use Entity Framework Core. Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.

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.

Prerequisites

To complete this tutorial, you need Visual Studio with the following workloads installed:

  • .NET desktop development
  • Data storage and processing

To install them, open Visual Studio Installer and choose Modify (or More > Modify) next to the version of Visual Studio you want to modify. See Modify Visual Studio.

Create a new project

Create a new Windows Forms App project for either C# or Visual Basic. Name the project ReadingXML.

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.

  1. On the Project menu, select Add New Item.

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

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

  3. Paste the following XML data 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, select Save authors.xml.

Create the user interface

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 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 receives the XML data

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

  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 select OK.

    DataSet1 is added to the component tray.

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

Create the event handler to read the XML file into the dataset

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.

  1. In Solution Explorer, select Form1, and then select 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 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's filled with the schema and is displayed in the TextBox control.

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

  2. Double-click the Show Schema button.

    The Code Editor opens at the ShowSchemaButton_Click event handler.

  3. Paste 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();
    }
    

Test the form

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

  1. Select F5 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.

Next steps

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.