For a Microsoft Visual C# .NET version of this
article, see
311566.
For a Microsoft Visual C++
.NET version of this article, see
311570.
For a Microsoft Visual Basic 6.0 version of this
article, see
263247.
This article refers to the following
Microsoft .NET Framework Class Library namespaces:
- System.Data.SqlClient
- System.IO
IN THIS TASK
SUMMARY
This article demonstrates how to read Extensible Markup
Language (XML) data into an ADO.NET
DataSet object.
back to the top
Requirements
The following list outlines the recommended hardware, software,
network infrastructure, and service packs that you need:
- Microsoft Windows 2000 Professional, Windows 2000 Server,
Windows 2000 Advanced Server, or Windows NT 4.0 Server
- Microsoft Visual Studio .NET
This article assumes that you are familiar with the following
topics:
- Visual Studio .NET
- ADO.NET fundamentals and syntax
- XML fundamentals
back to the top
Description of the Technique
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.
back to the top
Create Project and Add Code
This example uses a file named MySchema.xml. To create
MySchema.xml, follow the steps in the following Microsoft Knowledge Base
article:
308064 HOW TO: Persist an ADO.NET DataSet as XML Using Visual Basic .NET
The following code sample demonstrates how to use
two of the commonly used overloaded versions of
ReadXml. For other examples, refer to MSDN for individual overload topics
of this method.
- Start Visual Studio .NET.
- Create a new Windows Application project in Visual Basic
.NET. Form1 is added to the project by default.
- Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does
not.
- Place two Button controls and one DataGrid control on Form1. Change the Name property of Button1 to btnReader, and
change its Text property to Reader.
Change the Name property of Button2 to btnFile, and change
its Text property to File. - Use the Imports statement on the System, System.Data, and System.Data.SqlClient namespaces so that you are not required to qualify declarations
in those namespaces later in your code.
Imports System
Imports System.Data
Imports System.Data.SqlClient
- 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 Try
End 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 Try
End 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.
back to the top
Additional Notes
- To read only the XML schema, you can use the ReadXmlSchema method.
- To get only the XML representation of the data in the DataSet instead of persisting it onto a stream or a file, you can use the
GetXml method.
back to the top
REFERENCES
For additional information, click the
article numbers below to view the articles in the Microsoft Knowledge Base:
308064 HOW TO: Persist an ADO.NET DataSet as XML Using Visual Basic .NET
262450 HOWTO: A C++ Sample of ADO Recordset XML Persistence
For more information about ADO.NET objects and
syntax, refer to the following Microsoft .NET Framework Software Development
Kit (SDK) documentation or MSDN Online:
back to the top