You can use the ReadXml method to read XML schema and data into a DataSet. XML data can be read directly from a file, a Stream object, an XmlWriter object, or a TextWriter object.
You can use one of two sets of overloaded methods for the ReadXml method, depending on your needs. The first set of four overloaded methods takes just one parameter. The second set of four overloaded methods take an additional parameter (XmlReadMode) along with one of the parameters from the first set.
The following list outlines the first set of overloaded methods, which take one parameter:
The code to follow uses a specified file to read XML schema and data into the DataSet:
Overloads Public Sub ReadXml(String)
The code to follow uses a specified TextReader to read XML schema and data into the DataSet. TextReader is designed for character input.
Overloads Public Sub ReadXml(TextReader)
The code to follow uses a specified System.IO.Stream to read XML schema and data into the DataSet. The Stream class is designed for byte input and output.
Overloads Public Sub ReadXml(Stream)
The code to follow uses a specified XmlReader to read XML schema and data into the DataSet. This method provides fast, non-cached, forward-only access to XML data that conforms to the World Wide Web Consortium (W3C) XML 1.0 specification and the namespaces in the XML specification.
Overloads Public Sub ReadXml(XmlReader)
The list to follow outlines the second set of overloaded methods, which take XmlReadMode with one of the above-mentioned parameters. The XmlReadMode enumeration specifies how to read XML data and schema into a DataSet.
DiffGram. Reads a DiffGram, and applies changes from the DiffGram to the DataSet.
Fragment. Reads XML documents that contain inline XML-Data Reduced (XDR) schema fragments (such as those that are generated when you run FOR XML schemas that include inline XDR schema against an instance of Microsoft SQL Server).
IgnoreSchema. Ignores any inline schema and reads data into the existing DataSet schema.
InferSchema. Ignores any inline schema, infers schema from the data, and loads the data. If the DataSet already contains a schema, InferSchema extends the current schema by adding columns to tables that exist and by adding new tables if tables do not exist.
ReadSchema. Reads any inline schema, and loads the data.
Auto. Default. Performs the most appropriate action.
In the Code window, add the following code after the "Windows Form Designer generated code" region:
Private Sub btnReader_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnReader.Click Dim myXMLfile As String = "C:\MySchema.xml" Dim ds As New DataSet() ' Create new FileStream with which to read the schema. Dim fsReadXml As New System.IO.FileStream _ myXMLfile, System.IO.FileMode.Open) Try ds.ReadXml(fsReadXml) DataGrid1.DataSource = ds DataGrid1.DataMember = "Cust" Catch ex As Exception MessageBox.Show(ex.ToString()) Finally fsReadXml.Close() End TryEnd Sub Private Sub btnFile_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnFile.Click Dim myXMLfile As String = "C:\MySchema.xml" Dim ds As New DataSet() Try ds.ReadXml(myXMLfile) DataGrid1.DataSource = ds DataGrid1.DataMember = "Cust" Catch ex As Exception MessageBox.Show(ex.ToString()) End TryEnd Sub
Modify the path to the XML file (myXMLfile) as appropriate for your environment.
Save your project. On the Debug menu, click Start to run your project.
Click any of the buttons to read the XML data from the specified file. Notice that the XML data appears in the grid.