Microsoft KB Archive/309183

= How To Persist an ADO.NET DataSet as XML by Using Visual C# .NET =

Article ID: 309183

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 Q309183



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

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

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

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

REFERENCES



SUMMARY
This article demonstrates how to persist an ADO.NET DataSet object to Extensible Markup Language (XML).

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 WriteXml method to write XML schema and data from the DataSet object. The XML data is written to a file, a Stream class, an XmlWriter class, or a TextWriter class. You can use one of two sets of overloaded methods for WriteXml, depending on your needs. The first set of four overloaded methods takes just one parameter, and the second set of four overloaded methods takes an additional parameter (XmlWriteMode) along with one of the above-mentioned parameters. Each of these methods is described in this section.

To write the current schema and data for the DataSet to the specified file, use the following code: void DataSet.WriteXml(string fileName) To write the current schema and data for the DataSet, use the specified System.IO.TextWriter class. The TextWriter class is designed for character output. void DataSet.WriteXml(System.IO.TextWriter writer) To write the current schema and data for the DataSet, use the specified System.IO.Stream. The Stream class is designed for byte input and output. void DataSet.WriteXml(System.IO.Stream stream) To write the current schema and data for the DataSet to the specified System.Xml.XmlWriter, use the following code. This provides a fast, non-cached, forward-only method to generate streams or files that contain XML data to conform to the World Wide Web Consortium (W3C) XML 1.0 specification and the namespaces in the XML specification. void DataSet.WriteXml(Sytem.Xml.XmlWriter writer) The System.Data.XmlWriteMode enumeration specifies how to write XML data and schema from the DataSet. XmlWriteMode includes the following options:
 * DiffGram: Writes the entire DataSet as a DiffGram.
 * IgnoreSchema: Writes the current contents of the DataSet as XML data, without an XML Schema Definition language (XSD) schema.
 * WriteSchema: Writes the current contents of the DataSet as XML data with the relational structure as inline XSD schema.

back to the top

Create Project and Add Code
The following sample creates a DataSet from the customer table in the Northwind database and uses the WriteXml method to persist the DataSet into XML. This sample demonstrates how to use two of the commonly used overloaded versions of WriteXml. For other examples, refer to MSDN for individual overload topics of this method.  Start Microsoft Visual Studio .NET. Create a new Windows Application project in Visual C# .NET. 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 on Form1. Change the Name property of the first button to btnWriter and its Text property to Writer.

Change the Name property of the second button to btnFile and its Text property to File .  Use the using statement on the System and System.Data 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;   Copy and paste the following code in the event handler for the two buttons: private void btnWriter_Click(object sender, System.EventArgs e) {

string myConnectionString = &quot;User ID=login;password=password;Initial Catalog=Northwind; Data Source=servername&quot;; string mySelectQuery = &quot;Select * From Customers&quot;; System.IO.FileStream myFileStream = new System.IO.FileStream (&quot;c:\\mySchema.xml&quot;, System.IO.FileMode.Create); System.Xml.XmlTextWriter MyXmlTextWriter = new System.Xml.XmlTextWriter (myFileStream, System.Text.Encoding.Unicode); try {

SqlConnection con = new SqlConnection(myConnectionString); SqlDataAdapter daCust = new SqlDataAdapter(mySelectQuery, con); DataSet ds = new DataSet; daCust.Fill(ds, &quot;Cust&quot;);

//Write the XML along with the inline schema (default). ds.WriteXml(MyXmlTextWriter, XmlWriteMode.WriteSchema); //Write the XML only. //ds.WriteXml(MyXmlTextWriter, XmlWriteMode.IgnoreSchema); //Write the XML as a DiffGram. //ds.WriteXml(MyXmlTextWriter, XmlWriteMode.DiffGram); MessageBox.Show(&quot;Save complete&quot;); } catch (System.Exception ex) { MessageBox.Show(ex.ToString); }

finally { MyXmlTextWriter.Close; myFileStream.Close; } } private void btnFile_Click(object sender, System.EventArgs e) { string myConnectionString = &quot;User ID=login;password=password;Initial Catalog=Northwind; Data Source=servername&quot;; string mySelectQuery = &quot;Select * From Customers&quot;; try { SqlConnection con = new SqlConnection(myConnectionString); SqlDataAdapter daCust = new SqlDataAdapter(mySelectQuery, con); DataSet ds = new DataSet; daCust.Fill(ds, &quot;Cust&quot;);

//Write the XML along with the inline schema (default). ds.WriteXml(&quot;c:\\mySchema.xml&quot;, XmlWriteMode.WriteSchema); //Write the XML only. //ds.WriteXml(&quot;c:\\mySchema.xml&quot;, XmlWriteMode.IgnoreSchema); //Write the XML as a DiffGram. //ds.WriteXml(&quot;c:\\mySchema.xml&quot;, XmlWriteMode.DiffGram); MessageBox.Show(&quot;Save complete&quot;); }  catch (System.Exception ex) { MessageBox.Show(ex.ToString); } }                    Modify the connection string (myConnectionString) and XML file path (myXMLfile) as appropriate for your environment.</li> Save your project.</li> On the Debug menu, click Start to run your project.</li> Click any of the buttons to persist the data into the file.</li> Open your browser, and then open the XML file. Notice that the data in the DataSet has been persisted successfully in XML format based on the XmlWriteMode that you specified.</li></ol>

back to the top