首页 新闻 会员 周边

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

0
悬赏园豆:20 [已解决问题] 解决于 2010-06-29 16:33
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”。

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

问题补充: 麻烦帮帮看下!!
youngkeen的主页 youngkeen | 初学一级 | 园豆:8
提问于:2010-06-26 18:45
< >
分享
最佳答案
0

System.Xml.XmlTextReader rd;
rd
= (System.Xml.XmlTextReader)cmd.ExecuteXmlReader();

这个地方把XmlTextReader 改成

 System.Xml.XmlReader rd;

rd = cmd.ExecuteXmlReader();

收获园豆:20
leo刘 | 初学一级 |园豆:156 | 2010-06-27 11:38
其他回答(1)
0

这样写很麻烦,你可以试用一下LINQ那样产生XML非常的简单

xhswzx | 园豆:146 (初学一级) | 2010-06-27 13:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册