JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Writing Datasets to XML and Reading Datasets from XML

As you know, the data in datasets is transported using XML. In this topic, I'll take a closer look at that XML, using the DataSetXML example on the CD-ROM. This example writes the authors table of the pubs database to an XML file, dataset.xml, using the WriteXml method, and then reads that file back into a second dataset using the ReadXml method, as you see in Figure 22.4.


Figure 22.4: Writing a dataset's XML and reading that XML back in.

Here's the code—when the user clicks the "Write existing dataset to XML file" button, the authors table in the DataSet11 dataset is written to dataset.xml, and when the user clicks the "Create new dataset from XML file" button, a new dataset is created and reads its data in from dataset.xml:

Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    DataSet11.Clear()
    OleDbDataAdapter1.Fill(DataSet11)
    DataSet11.WriteXml("dataset.xml")
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
    Dim ds As New DataSet()
    ds.ReadXml("dataset.xml")
    DataGrid1.SetDataBinding(ds, "authors")
End Sub

You can see the dataset's data in the dataset.xml file (another file, DataSet1.xsd, will hold the XML schema, because this is a typed dataset):

<?xml version="1.0" standalone="yes"?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
  <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>Marjorie</au_fname>
    <phone>415 986-7020</phone>
        
Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor