获取一条记录的数据并生成XML文档

 1
 2    //获取一条记录的数据并生成XML文档
 3    private void ExportOne(string KeyID)
 4    {
 5        string strSQL = "Select * From PO Where HmNumPO='" + KeyID + "'";
 6
 7        SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=CSS;Persist Security Info=True;User ID=sa;Password=123456");
 8        SqlCommand cmd = new SqlCommand(strSQL, connection);
 9
10        connection.Open();
11        SqlDataReader rdr = cmd.ExecuteReader();
12        rdr.Read();
13
14        Response.Clear();
15        Response.Buffer = true;
16        Response.ContentType = "text/xml";
17        Response.Charset = "utf-8";
18
19
20        Response.AppendHeader("Content-Disposition""attachment;filename=" + rdr["HmNumPO"].ToString() + ".xml");
21        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
22        Response.ContentEncoding = System.Text.Encoding.UTF8;
23        //Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
24        //DBNull tmpDBNull = new DBNull();
25        
26
27        Response.Write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
28        Response.Write("<Root>");
29        Response.Write("<Title></Title>");
30        Response.Write("<Content>");
31        Response.Write("<Section4>");
32        Response.Write("    <Date>" + rdr["HmNumPO"].ToString() + "</Date>");
33        Response.Write("</Section4>");
34        Response.Write("<Section6>");
35        Response.Write("    <SupplierName>" + rdr["CostcoPONum"].ToString()  + "</SupplierName>");
36        Response.Write("    <QuoteProvidedBy>" + rdr["SHCity"].ToString() + " Liu</QuoteProvidedBy>");
37        Response.Write("    <Position>" + rdr["SHZip"].ToString() + "</Position>");
38        Response.Write("</Section6>");
39        Response.Write("<Section7>");
40        Response.Write("   <Phone>" + rdr["SHPhone"].ToString() + "</Phone>");
41        Response.Write("    <Email>" + rdr["SHEmail"].ToString() + "</Email>");
42        Response.Write("</Section7>");
43        Response.Write("<Section9>");
44        Response.Write("    <Description>" + rdr["CostcoPONum"].ToString() + "</Description>");
45        Response.Write("</Section9>");
46        Response.Write("<Section10>");
47        Response.Write("    <Description>" + rdr["CostcoPONum"].ToString() + "</Description>");
48        Response.Write("</Section10>");
49
50        Response.Write("</Content>");
51        Response.Write("</Root>");
52
53
54        Response.Flush();
55        Response.End();        
56    
57    }

58
59
原文地址:https://www.cnblogs.com/liuweitoo/p/914825.html