本文实例讲述了C#从数据库读取数据到DataSet并保存到xml文件的方法。分享给大家供大家参考。具体实现方法如下:
DataSet有一个WriteXml方法可以直接将数据保存到xml文件
using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.IO; public class TestWriteXML { public static void Main() { String strFileName = c:/temp/out.xml; SqlConnection conn = new SqlConnection(server=localhost;uid=sa;pwd=;database=db); String strSql = SELECT name,age FROM people; SqlDataAdapter adapter = new SqlDataAdapter(); adapter.SelectCommand = new SqlCommand(strSql, conn); // Build the DataSet DataSet ds = new DataSet(); adapter.Fill(ds, employees); // Get a FileStream object FileStream fs = new FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write); // Apply the WriteXml method to write an XML document ds.WriteXml(fs); fs.Close(); } }
希望本文所述对大家的C#程序设计有所帮助。