using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default3 : System.Web.UI.Page { private string strCon = ConfigurationManager.ConnectionStrings[1].ConnectionString; private string sql = "select ArticleId,ArticleOrder,Type,DealDeparment,AddTime,Content from tbLeaveWordArticles"; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = GetDataTable(sql); //无法序列化 DataTable。未设置 DataTable 名称。 //或者改用DataSet,DataSet是XML Webservice能够支持的最小的可序列化对象 dt.TableName = "tbLeaveWordArticles"; string Path = "d://tel2.xml"; OutDataToXML(dt, Path); } } /// <summary> /// 导出数据到xml文件中 /// </summary> /// <param name="dt">要导出的数据表</param> /// <param name="strXMLPath">导出到xml的文件路径</param> protected void OutDataToXML(DataTable dt, string strXMLPath) { dt.WriteXml(strXMLPath); } protected DataTable GetDataTable(string sql) { DataTable dt = new DataTable(); //异常处理 SqlConnection con = new SqlConnection(strCon); SqlCommand cmd = new SqlCommand(sql,con); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); con.Close(); return dt; } }