将数据库读取的表写入xml时出现错误!求帮帮看下~

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.XmlTextReaderImpl”的对象强制转换为类型“System.Xml.XmlTextReader”。

请问应该如何改这个错误呢???

鸿蒙传说
浏览 468回答 2
2回答

心有法竹

System.Xml.XmlTextReader rd; rd = (System.Xml.XmlTextReader)cmd.ExecuteXmlReader(); 这个地方把XmlTextReader 改成  System.Xml.XmlReader rd; rd = cmd.ExecuteXmlReader();

梵蒂冈之花

这样写很麻烦,你可以试用一下LINQ那样产生XML非常的简单
打开App,查看更多内容
随时随地看视频慕课网APP