这个是我写的代码,你把相应的名称改一下就可以了:
==========================================================
using System;
using System.Data;
using System.Data.SqlClient;
namespace WriteXML
{
class Program
{
static void Main(string[] args)
{
// connection string
string connString = @"
server = .\sql2005express;
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:\Documents and Settings\Jackey Tine\My Documents\Projects\productstable.xml");
Console.WriteLine("The file is Created");
}
catch (Exception e)
{
Console.WriteLine("Error: " + e);
}
finally
{
conn.Close();
}
}
}
}
=================================================================
显示效果如下:
1.从数据库中读取的数据存入dataset,dataset本身就是用XML格式存储的
SqlDataAdapter da=new SqlDataAdapter(cmd,conn);
Dataset ds=new Dataset();
da.fill(ds);
ds.WriteXml(多个重载方法,自己找);
楼上的 关键一句:ds.WriteXml(@"C:\productstable.xml");
WriteXml(url); 生成xml文件。
ReadXml(url); 读取xml 文件。