我用web service服务定义了一个结果集,大致如下:
    public string News()
    {
        SqlConnection con = DB.createCon();
        con.Open();
        SqlCommand cmd = new SqlCommand("select Title,Location,DateTime from News where Result='1'", con);
        return cmd.ExecuteReader().ToString();
        con.Close();
    }
调用的接口为:
http://localhost:1310/WEB-Site/Service/WebService.asmx/News
内容为:
  <?xml version="1.0" encoding="utf-8" ?> 
  <string xmlns="http://tempuri.org/">System.Data.SqlClient.SqlDataReader</string>打开接口看到的是System.Data.SqlClient.SqlDataReader,这样的数据集别人能抓到吗?还是说正确的应该是按规范的格式显示为:
标题:Title
地点:Location
时间:DateTime
这样的XML数据集格式?由于数据集不只是一条,可能多条,要让调用的接口打开后显示为规范的格式该如何写?

解决方案 »

  1.   

    ....你太有才了
     你最好用dataset返回,然后写一个dataset的xml序列化的类
      

  2.   

    我也是第一次做这个...
    dataset我很少用...
    写dataset的xml序列化的类就更陌生了...哪位好心能写点代码或实例我学习一下,谢谢了.
      

  3.   

    网上google应该有大把的
    序列化的原因是因为.WebService面向的调用是未知的,就是传说中的跨平台所以C#中有的类其它的开发语言没有,所以要序列化
      

  4.   

    找到个例子可惜是VB的,哪位帮我转换成C#的吧,谢谢了...<WebMethod(Description:="所有教室列表")> _
    Public Function ListAllRooms() As XmlDocumentTry
    m_CpCourseArrange.FillRoomId(m_DsCourseArrange)
    'Dim reader As New MemoryStream
    Dim doc As New XmlDocument
    doc.LoadXml(m_DsCourseArrange.GetXml.ToString) 
    Return docCatch ex As Protocols.SoapException
    Throw SoapExceptionE.RaiseException("ListAllRooms", "http://tempuri.org/CourseArrange", ex.Message, "4000", ex.Source, SoapExceptionE.FaultCode.Server)
    End Try
    End Function
    GetXML--Returns the XML representation of the data stored in the DataSet. (MSDN)
    Private Shared Sub DemonstrateGetXml()
    ' Create a DataSet with one table containing two columns and 10 rows.
    Dim ds As DataSet = New DataSet("myDataSet")
    Dim t As DataTable = ds.Tables.Add("Items")
    t.Columns.Add("id", Type.GetType("System.Int32"))
    t.Columns.Add("Item", Type.GetType("System.String"))' Add ten rows.
    Dim r As DataRow
    Dim i As Integer
    For i = 0 To 9
    r = t.NewRow()
    r("id") = i
    r("Item")= "Item" & i
    t.Rows.Add(r)
    Next' Display the DataSet contents as XML.
    Console.WriteLine( ds.GetXml() )
    End Sub
      

  5.   

    在网上找到有一个wawa写的例子namespace wawa 
    { [WebService(Name= "wawa Service", 
    Description="wawa", 
    Namespace="wawa") 

    public class wawaService : System.Web.Services.WebService 

    [WebMethod(Description="wawa")] 
    public DataSet wawa_getDataSet(string str) 
    { return getDataSet(str_wawa_sql); //但调试时显示当前上下文不存在名称str_wawa_sql,这里硬是看不明白...高人指点下吧...