如何仅提取和显示weatherzone中的当前温度?

时间:2016-03-29 09:12:53

标签: c# xml weather-api

通过C#代码从weatherzone获取数据:

string url = "http://rss.weatherzone.com.au/?u=12994-1285&lt=twcid&lc=160255&obs=1&fc=1";
XmlReader reader = XmlReader.Create(url);
SyndicationFeed feed = SyndicationFeed.Load(reader);
reader.Close();

获取当前天气:

var temperature = feed.Items.ToList()[0].Summary.Text.Trim();

输出:

<b>Temperature:</b> 33.0&#176;C
<img align="top" src="http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif" alt="steady"/>
<br />
<b>Feels like:</b> 38.0&#176;C<br />
<b>Dew point:</b> 23.0&#176;C
<img align="top" src="http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif" alt="steady"/>
<br />
<b>Relative humidity:</b> 56%<br />
<b>Wind:</b> NE at 18 km/h, gusting to  km/h
<img align="top" src="http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif" alt="steady"/>
<br />
<b>Rain:</b> mm since 9am<br />
<b>Pressure:</b> 1013.0 hPa
<img align="top" src="http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif" alt="steady"/>
<br />

问题:如何在此输出中获得温度?

2 个答案:

答案 0 :(得分:0)

你可以做这样粗暴的事情:

var stringTemp = "<b>Temperature:</b> 33.0&#176;C <img align=\"top\" src=\"http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif\" alt=\"steady\"/> <br /> <b>Feels like:</b> 38.0&#176;C<br /> <b>Dew point:</b> 23.0&#176;C <img align=\"top\" src=\"http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif\" alt=\"steady\"/> <br /> <b>Relative humidity:</b> 56%<br /> <b>Wind:</b> NE at 18 km/h, gusting to km/h <img align=\"top\" src=\"http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif\" alt=\"steady\"/> <br /> <b>Rain:</b> mm since 9am<br /> <b>Pressure:</b> 1013.0 hPa <img align=\"top\" src=\"http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif\" alt=\"steady\"/> <br />";
var start = stringTemp.IndexOf("</b>");
var end = stringTemp.IndexOf("<img");
var temp = stringTemp.Substring(start + "</b>".Length, end - start - "<img".Length);

答案 1 :(得分:0)

另一种方式

var temperature = "<b>Temperature:</b> 33.0&#176;C <img align=\"top\" src=\"http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif\" alt=\"steady\"/> <br /> <b>Feels like:</b> 38.0&#176;C<br /> <b>Dew point:</b> 23.0&#176;C <img align=\"top\" src=\"http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif\" alt=\"steady\"/> <br /> <b>Relative humidity:</b> 56%<br /> <b>Wind:</b> NE at 18 km/h, gusting to km/h <img align=\"top\" src=\"http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif\" alt=\"steady\"/> <br /> <b>Rain:</b> mm since 9am<br /> <b>Pressure:</b> 1013.0 hPa <img align=\"top\" src=\"http://www.weatherzone.com.au/images/widgets/nav_trend_steady.gif\" alt=\"steady\"/> <br />";

C#:

    private static string Temperature(string temperature)
    {
        try
        {
            string[] value = Regex.Split(temperature, "\n");
            string[] temp = Regex.Split(value[0], "</b> ");
            return temp[1].Substring(0, 2);
        }
        catch { return string.Empty; }
    }