我想把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求高人指点!!!