如何实现Xoap天气api

时间:2009-10-13 13:29:59

标签: c# web-services

我需要实施Xoap weather api。

请帮助代码。

2 个答案:

答案 0 :(得分:1)

检查link

尝试代码:

web.config中的

<appSettings>

<add key="PartnerID" value="xxx"/>
<add key="LicenseKey" value="xxx" />
<add key="ExtForecastLength" value="5" />

</appSettings>

<强> weather.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Weather.aspx.cs" Inherits="Weather" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Xml ID="Xml1" runat="server"></asp:Xml>
</div>
</form>
</body>
</html>

<强> Weather.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.Xml;
using System.IO;

public partial class Weather : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String reqUrl = "http://xoap.weather.com/weather/local/29206?cc=*&dayf=" +
ConfigurationSettings.AppSettings.Get("ExtForecastLength") +
"&prod=xoap&par=" + ConfigurationSettings.AppSettings.Get("PartnerID") +
"&key=" + ConfigurationSettings.AppSettings.Get("LicenseKey");
// First we request our content from our provider source .. in this case .. The Weather Channel
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(reqUrl);
//load the response into a response object
WebResponse resp = wr.GetResponse();
// create a new stream that can be placed into an XmlTextReader
Stream str = resp.GetResponseStream();
XmlTextReader reader = new XmlTextReader(str);
reader.XmlResolver = null;
// create a new Xml document
XmlDocument doc = new XmlDocument();
doc.Load(reader);
// set out object properties
Xml1.Document = doc;
Xml1.TransformSource = "XSLTFile.xslt";


}
}

<强> XSLTFile.xslt: -

<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/weather">
<table width="225px" border="0" cellSpacing="0" cellPadding="0" bgcolor="EFF5D7" ID="Table1">
<tr>
<td colspan="2" align="center" valign="center"
style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana">
<xsl:value-of select="loc/dnam"></xsl:value-of>
</td>
</tr>
<tr>
<xsl:variable name="med-img-dir">./images/weather/64x64</xsl:variable>
<xsl:variable name="img-ext">png</xsl:variable>
<xsl:variable name="iconnumber">
<xsl:value-of select="cc/icon"></xsl:value-of>
</xsl:variable>
<td height="64px" width="64px" align="center" valign="center">
<img src="{$med-img-dir}/{$iconnumber}.{$img-ext}"></img>
</td>
<td width="161px" align="center" valign="center" style="FONT-SIZE: 22pt;
FONT-WEIGHT: bold">
<xsl:value-of select="cc/tmp"></xsl:value-of>°F
</td>
</tr>
<tr>
<td align="center" valign="center" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana; HEIGHT: 33px">
<xsl:value-of select="cc/t"></xsl:value-of>
</td>
<td align="center" valign="center" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana; HEIGHT: 33px">
Feels like
<xsl:value-of select="cc/flik"></xsl:value-of>°F
</td>
</tr>
<table width="225px" border="0" cellSpacing="0" cellPadding="0" bgcolor="EFF5D7" ID="Table2">
<tr>
<td width="10%"></td>
<td width="50%" align="left" valign="center" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
UV Index:
</td>
<td width="40%" align="left" valign="center" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
<xsl:value-of select="cc/uv/i"></xsl:value-of>
<xsl:value-of select="cc/uv/t"></xsl:value-of>
</td>
</tr>
<tr>
<td width="10%"></td>
<td width="50%" align="left" valign="center" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
Dew Point:
</td>
<td width="40%" align="left" valign="center" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
<xsl:value-of select="cc/dewp"></xsl:value-of>°F
</td>
</tr>
<tr>
<td width="10%"></td>
<td width="50%" align="left" valign="center" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
Humidity:
</td>
<td width="40%" align="left" valign="center" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
<xsl:value-of select="cc/hmid"></xsl:value-of>%
</td>
</tr>
<tr>
<td width="10%"></td>
<td width="50%" align="left" valign="center" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
Visibility:
</td>
<td width="40%" align="left" valign="center" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
<xsl:value-of select="cc/vis"></xsl:value-of>miles
</td>
</tr>
<tr>
<td width="10%"></td>
<td width="50%" align="left" valign="top" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
Pressure:
</td>
<td width="40%" align="left" valign="top" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
<xsl:value-of select="cc/bar/r"></xsl:value-of>inches and
<xsl:value-of select="cc/bar/d"></xsl:value-of>
</td>
</tr>
<tr>
<td width="10%"></td>
<td width="50%" align="left" valign="top" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
Wind:
</td>
<td width="40%" align="left" valign="top" style="FONT-SIZE: 9pt; COLOR: black;
FONT-FAMILY: Verdana">
<xsl:value-of select="cc/wind/s"></xsl:value-of>mph
</td>
</tr>
</table>
<table width="225px" border="0" cellSpacing="0" cellPadding="0" bgcolor="EFF5D7" ID="Table3">
<tr>
<td colspan="5" width="100%" align="center"><u>Extended Forecast</u></td>
</tr>
<tr>
<xsl:for-each select="dayf/day">
<xsl:if test="@d > 0">
<td width="25%" align="center" valign="top" style="FONT-SIZE: 7pt; COLOR: black;
FONT-FAMILY: Verdana">
<xsl:value-of select="@t"></xsl:value-of>
</td>
</xsl:if>
</xsl:for-each>
</tr>
<tr>
<xsl:variable name="small-img-dir">./images/weather/32x32</xsl:variable>
<xsl:variable name="img-ext">png</xsl:variable>
<xsl:for-each select="dayf/day">
<xsl:if test="@d > 0">
<xsl:variable name="iconnumber">
<xsl:value-of select="part/icon"></xsl:value-of>
</xsl:variable>
<td width="25%" align="center" valign="center">
<img border="1" src="{$small-img-dir}/{$iconnumber}.{$img-ext}"></img>
</td>
</xsl:if>
</xsl:for-each>
</tr>
<tr>
<xsl:for-each select="dayf/day">
<xsl:if test="@d > 0">
<td width="25%" align="center" valign="top" style="FONT-SIZE: 7pt; COLOR: black;
FONT-FAMILY: Verdana">
High:
<xsl:value-of select="hi"></xsl:value-of>
</td>
</xsl:if>
</xsl:for-each>
</tr>
<tr>
<xsl:for-each select="dayf/day">
<xsl:if test="@d > 0">
<td width="25%" align="center" valign="top" style="FONT-SIZE: 7pt; COLOR: black;
FONT-FAMILY: Verdana">
Low:
<xsl:value-of select="low"></xsl:value-of>
</td>
</xsl:if>
</xsl:for-each>
</tr>
<tr>
<xsl:for-each select="dayf/day">
<xsl:if test="@d > 0">
<td width="25%" align="center" valign="top" style="FONT-SIZE: 7pt; COLOR: black;
FONT-FAMILY: Verdana">
<xsl:value-of select="part/t"></xsl:value-of>
</td>
</xsl:if>
</xsl:for-each>
</tr>
</table>
<table width="225px" border="0" cellSpacing="0" cellPadding="4" bgcolor="EFF5D7" ID="Table4">
<tr>
<xsl:variable name="twclink">http://www.weather.com/?prod=xoap</xsl:variable>
<td width="20%" align="center" valign="top" style="FONT-SIZE: 7pt; COLOR: black;
FONT-FAMILY: Verdana">
Weather data provided by<a href="{$twclink}" target="_none">
<img border="0" src="./images/weather/logos/TWClogo_32px.png"></img></a>
</td>
</tr>
</table>
</table>
</xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

weather.com的另一个.NET API

http://weatherclient.codeplex.com