从数据库中检索Xml数据到我的页面

时间:2012-06-26 10:03:04

标签: c# sql-server

我正在尝试使用存储过程从数据库中将数据检索到xml标记中。我想像标准的xml响应一样创建响应。但我得到一个名为<NewDataSet/>

的标签

这是我检索数据的代码:

comm = new SqlCommand("[sp_get_business_locations]", conn);
    comm.CommandType = System.Data.CommandType.StoredProcedure;
    comm.Parameters.AddWithValue("@Latitude", Latitude);
    comm.Parameters.AddWithValue("@Longitude", Longitude);
comm.ExecuteNonQuery();    
cdoc.LoadXml(shop.GetXml());

可以做些什么?感谢。

DataSet shop = new DataSet();
XmlDocument cdoc = new XmlDocument();

这是我的存储过程:

SElECT  @TotalRows=COUNT(*) FROM HeritageList   
 INNER JOIN  Reviews loc  WITH (NOLOCK) ON HeritageList.HeritageId = loc.HeritageId    
 WHERE ([dbo].[DistanceBetween] (@Latitude,@Longitude,Latitude,Longitude)) * @meters <= @Radius      
        
    IF(@TotalRows>0)    
    BEGIN    
         DECLARE @Partialxml XML      
     DECLARE @Totalxml XML      
     declare @Appendxml VARCHAR(100)       
     SET @Appendxml = '<HeritageList></HeritageList>';      
     set @Totalxml=@Appendxml      
         
     SET @Partialxml = (SELECT  HeritageList.HeritageId,
           
        
         
        Latitude as "Location/Latitude",      
        Longitude as "Location/Longitude",      
        Country as "Location/Country",      
        [State] as "Location/State",      
        City as "Location/City",      
        Postcode as "Location/Postcode",      
        [Address] as "Location/Address",     
        (Cast(Round((( [dbo].[DistanceBetween]           
         (@Latitude,@Longitude,Latitude,Longitude)) * @meters),2,1) as decimal(18,2))) as Distance    
        FROM dbo.HeritageList  INNER JOIN  Reviews loc   WITH (NOLOCK) ON HeritageList.HeritageId = loc.HeritageId    
        WHERE ([dbo].[DistanceBetween] (@Latitude,@Longitude,Latitude,Longitude)) * @meters <= @Radius     
          FOR XML PATH('Business'), ELEMENTS XSINIL)      
     SET @Totalxml.modify('insert sql:variable("@Partialxml")  as last into (/HeritageList)[1] ' )      
      
     SELECT @Totalxml      
 END    
    
END 

1 个答案:

答案 0 :(得分:0)

构建DataAdapter,

SqlDataAdapter adpater=new SqlDataAdapter(comm);
DataSet shop=new DataSet();
adapter.Fill(shop);

XmlDocument cdoc = new XmlDocument();
cdoc.LoadXml(shop.GetXml());