Save data stored in table to xml file : ResultSet to Xml « ADO.Net « C# / CSharp Tutorial






using System;
using System.Data;
using System.Data.SqlClient;

class MainClass
{
   static void Main(string[] args)
   {
      string connString = @"server = .\sqlexpress;integrated security = true;database = northwind";

      string qry = @"select productname,unitprice from products";

      SqlConnection conn = new SqlConnection(connString);

      try
      {
         SqlDataAdapter da = new SqlDataAdapter();
         da.SelectCommand = new SqlCommand(qry, conn);

         conn.Open();

         DataSet ds = new DataSet();
         da.Fill(ds, "products");

         ds.WriteXml(@"c:\productstable.xml");
      }
      catch(Exception e)
      {
         Console.WriteLine("Error: " + e);
      }
      finally
      {
         conn.Close();
      }
   }
}








32.51.ResultSet to Xml
32.51.1.SqlCommand.ExecuteXmlReader
32.51.2.Convert the result from a query to XML and output
32.51.3.Convert the result from a query to XML and output (Disconnected mode)
32.51.4.Save data stored in table to xml file