把sql server转换成XML出错

ceachother 2012-06-09 04:27:28
我想把sql server中的表转换成xml,于是在vs新建了“ASP.NET Web服务应用程序”。
在web service中代码如下
[WebMethod(Description = "get content from SQLSERVER",CacheDuration = 600, MessageName = "GetContent")]
public XmlDataDocument GetContent()
{
SqlConnection SqlConn;
string connectionString = "Server=Cecil-PC\\SQLSERVER2005;User Id=sa;Pwd=;DataBase=test0317";
SqlConn = new SqlConnection(connectionString);
//string commandstring = "select s_name,s_birthday,s_native_place from dbo.staff order by s_id FOR XML AUTO";
string commandstring = "select * from dbo.staff for xml auto";
SqlCommand command = new SqlCommand(commandstring, SqlConn);
SqlConn.Open();
XmlReader reader = command.ExecuteXmlReader();
System.Web.HttpContext.Current.Response.Write(Server.HtmlEncode(reader.ReadOuterXml() + "<br>"));
reader.Read();
XmlDataDocument xml = new XmlDataDocument();
xml.Load(reader);
reader.Close();
return xml;
}


运行后报错为:
System.InvalidOperationException: 此文档已具有“DocumentElement”节点。
在 System.Xml.XmlDocument.IsValidChildType(XmlNodeType type)
在 System.Xml.XmlDocument.AppendChildForLoad(XmlNode newChild, XmlDocument doc)
在 System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
在 System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
在 System.Xml.XmlDocument.Load(XmlReader reader)
在 System.Xml.XmlDataDocument.Load(XmlReader reader)
在 WebService1.Service1.GetContent() 位置 D:\Program\2012\0608\WebService1\WebService1\Service1.asmx.cs:行号 53

求高人指点!!!
...全文
115 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ceachother 2012-06-10
  • 打赏
  • 举报
回复
有人能帮忙么

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧