//Connect to the data source.
OleDbConnection objConn = new OleDbConnection (strConn);
try
{
objConn.Open();
//Fill a dataset with records from the Customers table.
OleDbCommand objCmd = new OleDbCommand(
"Select CustomerID, CompanyName, ContactName, "
+ "Country, Phone from Customers", objConn);
OleDbDataAdapter objAdapter = new OleDbDataAdapter();
objAdapter.SelectCommand = objCmd;
DataSet objDataset = new DataSet();
objAdapter.Fill(objDataset);
//Create the FileStream to write with.
System.IO.FileStream fs = new System.IO.FileStream(
"C://Customers.xml", System.IO.FileMode.Create);
//Create an XmlTextWriter for the FileStream.
System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(
fs, System.Text.Encoding.Unicode);
//Add processing instructions to the beginning of the XML file, one
//of which indicates a style sheet.
xtw.WriteProcessingInstruction("xml", "version='1.0'");
//xtw.WriteProcessingInstruction("xml-stylesheet",
// "type='text/xsl' href='customers.xsl'");
//Write the XML from the dataset to the file.
objDataset.WriteXml(xtw);
xtw.Close();
//Close the database connection.
objConn.Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}