您的位置:首页 > 编程语言 > ASP

asp.net web服务程序输出xml格式文档

2013-03-09 19:49 633 查看
其实webservice并不是那么的神秘,它也不过只是个接口,对我们而言,侧重点依然是接口函数的编写.下面,我将给出我们的例子所需要的接口函数.

  [WebMethod(Description="查询以获取需要的课件信息")]

  public XmlDataDocument GetSiteAData(string AssignName)

  {

  XmlDataDocument xd=new XmlDataDocument(); //

  DataSet ds=new DataSet();

  CStoreProc cp=new CStoreProc("SearchAssign");

  cp.AddParIn("@keywords",SqlDbType.VarChar,30,AssignName);

  cp.AddParOut("@res",SqlDbType.Int);

  if(cp.SelectProc()) //如果执行成功,存储过程

  {

  cp.myData.EnforceConstraints=false; //不进行格式严格检查

  if((int)cp.GetReturnValue("@res")==-1)

  {

  string xml="";

  xd.LoadXml(xml);

  return xd;

  }

  xd=new XmlDataDocument(cp.myData);

  XmlNode root1=xd.DocumentElement;

  XmlNodeList roots=root1.SelectNodes("list");

  foreach(XmlNode roota in roots) //为所有元素加上站点名称标记

  {

  XmlElement Link=xd.CreateElement("SiteName");

  Link.InnerText=ConfigurationSettings.AppSettings["SiteName"].ToString();

  roota.AppendChild(Link);

  }

  return xd;

  }

  else return null;

  }

  这是获取资源站点信息的一个接口函数.里面大部分的代码,我想对于有一定asp.net基础的朋友来说,都应该是一看就明白,这里只说明下CStoreProc,这是我封装的一个存储过程类,主要功能是执行各种类型的存储过程.

  细心的朋友可能会发现这个函数的返回类型似乎比较特殊,是个xml的文档.我们在前面已经说过,webservice只能传输序列化数据,xml显然满足 条件,但比如hash表之类的非

序列化数据,是不能传输的,xml使用最为广泛,而且考虑到跨平台应用,所以这里我们只以xml数据的传输来示例.

本人示例代码:

[WebMethod(Description = "从雨量数据库中返回雨量值")]

public XmlDataDocument getRainVlue()

{

if (IsCanConnectioned==true)

{

string sqlcmd=string.Format("SELECT DISTINCT dbo.RAIN_REALTIME.GprsID, ABS(dbo.RAIN_REALTIME.RainValue - 100) / 10 AS rainValue, dbo.USERMESSAGE.StationName, dbo.USERMESSAGE.subdistrict, dbo.USERMESSAGE.StationAddress, dbo.RAIN_REALTIME.DataTime
FROM dbo.RAIN_REALTIME INNER JOIN dbo.USERMESSAGE ON dbo.RAIN_REALTIME.GprsID = dbo.USERMESSAGE.StationNo");

SqlDataAdapter sda = new SqlDataAdapter(sqlcmd, strConnection);

DataSet ds = new DataSet();

sda.Fill(ds);

XmlDataDocument xd = new XmlDataDocument(ds);

return xd;

}

else

{

return null;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐