Microsoft KB Archive/311566

= How To Read XML Data into a DataSet by Using Visual C# .NET =

Article ID: 311566

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft ADO.NET 1.1
 * Microsoft ADO.NET 1.0
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q311566



For a Microsoft Visual Basic .NET version of this article, see 309702.

For a Microsoft Visual C++ .NET version of this article, see 311570.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Data
 * System.Data.SqlClient

IN THIS TASK
SUMMARY
 * Requirements
 * Description of the Technique
 * Create Project and Add Code
 * Additional Notes

REFERENCES



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.</li> Make sure that your project contains a reference to the System.Data namespace, and add a reference to this namespace if it does not.</li> 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 .</li>  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; </li>  Add the following code in the event handler that corresponds to the buttons: private void btnReader_Click(object sender, System.EventArgs e) { string myXMLfile = @&quot;C:\MySchema.xml&quot;; 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 = &quot;Cust&quot;; }   catch (Exception ex) {   MessageBox.Show(ex.ToString); }   finally {   fsReadXml.Close; } } private void btnFile_Click(object sender, System.EventArgs e) { string myXMLfile = &quot;C:\\MySchema.xml&quot;; DataSet ds = new DataSet; try {       ds.ReadXml(myXMLfile); dataGrid1.DataSource = ds; dataGrid1.DataMember = &quot;Cust&quot;; }   catch (Exception ex) {       MessageBox.Show(ex.ToString); } }                   </li> Modify the path to the XML file (MyXmlFile) as appropriate for your environment.</li> Save your project. On the Debug menu, click Start to run your project.</li> Click any of the buttons to read the XML data from the specified file. Notice that the XML data appears in the grid.</li></ol>

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

<div class="references_section">