我被这个问题困扰了几个小时,似乎无法解决,所以我在这里问一下:)
好的,我有这个函数:
private void XmlDump()
{
XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
XElement rootElement = new XElement("dump");
rootElement.Add(TableToX("Support"));
string connectionString = ConfigurationManager.ConnectionStrings["MyDb"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
string sql = "select * from support";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet("Test");
da.Fill(ds, "support");
// Convert dataset to XML here
var docresult = // Converted XML
Response.Write(docResult);
Response.ContentType = "text/xml; charset=utf-8";
Response.AddHeader("Content-Disposition", "attachment; filename=test.xml");
Response.End();
}
我尝试了各种不同的方法,但是一直出现错误,所以我把如何将DataSet转换为XML的部分留空了。
还有一件事,这个查询包含带特殊字符的列。
Encoding.UTF8
。正在更新代码。 - Abdul MunimToArray()
你是不是想说ToString()
? - Jay Croghan