public static void create_xml(string sql, string top_node, string xml_filename)
{
String se = System.AppDomain.CurrentDomain.BaseDirectory;
string consql = ConfigurationSettings.AppSettings["consql"].ToString();
SqlConnection con = new SqlConnection(consql);
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
System.Xml.XmlTextReader rd;
rd = (System.Xml.XmlTextReader)cmd.ExecuteXmlReader();
System.IO.StreamWriter w = new System.IO.StreamWriter(xml_filename, false, System.Text.Encoding.GetEncoding("gb2312"));
w.WriteLine("<?xml version='1.0'encoding='gb2312'?>");
w.WriteLine("<"+top_node+">");
while (rd.IsStartElement())
w.WriteLine(rd.ReadOuterXml());
w.WriteLine("</"+top_node+">");
w.Close();
rd.Close();
con.Close();
}
下面是调用方法的代码
protected void Button5_Click(object sender, EventArgs e)
{
string xml = TextBox5.Text;
string gen = "stundet";
stat.create_xml("select stor_id,stor_name,stor_address from stores for xml auto",gen,Server.MapPath(""+xml+".xml"));
}
运行后出现这样错误
请问应该如何改这个错误呢???
System.Xml.XmlTextReader rd;
rd = (System.Xml.XmlTextReader)cmd.ExecuteXmlReader();
这个地方把XmlTextReader 改成
System.Xml.XmlReader rd;
rd = cmd.ExecuteXmlReader();
这样写很麻烦,你可以试用一下LINQ那样产生XML非常的简单