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:
309183 How To Persist an ADO.NET DataSet as XML by Using Visual C# .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 C# .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 using 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.
using System;
using System.Data;
using System.Data.SqlClient;
- Add the following code in the event handler that corresponds to the buttons:
private void btnReader_Click(object sender, System.EventArgs e)
{
string myXMLfile = @"C:\MySchema.xml";
DataSet ds = new DataSet();
// Create new FileStream with which to read the schema.
System.IO.FileStream fsReadXml = new System.IO.FileStream
(myXMLfile, System.IO.FileMode.Open);
try
{
ds.ReadXml(fsReadXml);
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "Cust";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
fsReadXml.Close();
}
}
private void btnFile_Click(object sender, System.EventArgs e)
{
string myXMLfile = "C:\\MySchema.xml";
DataSet ds = new DataSet();
try
{
ds.ReadXml(myXMLfile);
dataGrid1.DataSource = ds;
dataGrid1.DataMember = "Cust";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
- 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:
309183 How To Persist an ADO.NET DataSet as XML by Using Visual C# .NET
262450 How To 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