(Exercise 23.8, page 974) Investigate other web services at a site like http://w
ID: 3837523 • Letter: #
Question
(Exercise 23.8, page 974) Investigate other web services at a site like http://www.programmableweb.com. Locate a RESTful web service that returns XML, then modify the example of Fig. 23.4 to invoke the web service asynchronously using the methods of HttpClient. Parse the results using LINQ to XML, then display the results as appropriate for the type of data returned.
One sample source of data can be found at: Yahoo Weather API
(Generated with: https://developer.yahoo.com/weather/)
You could query the local weather for Brentwood, then selecting Descendants with the name "yweather:forecast" you could get the forecast for the next few days.
Or at least I thought you could in C# using XDocument, but it turns out having a namespace (the 'yweather') requires some additional code to work. Here is the necessary code to use the Yahoo Weather XML. The actual namespace URL is found inside the XML document if you view the source. The C# code is below:
XNamespace ns = "http://xml.weather.yahoo.com/ns/rss/1.0";
foreach (var element in theXML.Descendants(ns + "forecast"))
{
Console.Out.WriteLine(element);
}
Feel free to do this as a console application or a GUI application. Your choice!
e http://localhost 52020/WebTime.aspx p c x a Simple Web Form Example x Current time on the Web server: 05:29:02 Firefox Simple Web Form Example 3 ocalhost: WebTime.aspx 52020 Google Feedback 2 Disable- Cookies css- Forms. IL Images. 0 Information Miscellaneous outline Resize T Current time on the Web server: 05:29:03Explanation / Answer
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>Yahoo! Weather - Sunnyvale, CA</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html</link>
<description>Yahoo! Weather for Sunnyvale, CA</description>
<language>en-us</language>
<lastBuildDate>Fri, 18 Dec 2009 9:38 am PST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Sunnyvale" region="CA" country="United States"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="50" direction="0" speed="0" />
<yweather:atmosphere humidity="94" visibility="3" pressure="30.27" rising="1" />
<yweather:astronomy sunrise="7:17 am" sunset="4:52 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>https://s.yimg.com/zz/combo?a/i/us/nws/th/main_142b.gif</url>
</image>
<item>
<title>Conditions for Sunnyvale, CA at 9:38 am PST</title>
<geo:lat>37.37</geo:lat>
<geo:long>-122.04</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html</link>
<pubDate>Fri, 18 Dec 2009 9:38 am PST</pubDate>
<yweather:condition text="Mostly Cloudy" code="28" temp="50" date="Fri, 18 Dec 2009 9:38 am PST" />
<description><![CDATA[
<img src="https://s.yimg.com/zz/combo?a/i/us/we/52/28.gif"/><br />
<b>Current Conditions:</b><br />
Mostly Cloudy, 50 F<BR />
<BR /><b>Forecast:</b><BR />
Fri - Partly Cloudy. High: 62 Low: 49<br />
Sat - Partly Cloudy. High: 65 Low: 49<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]></description>
<yweather:forecast day="Fri" date="18 Dec 2009" low="49" high="62" text="Partly Cloudy" code="30" />
<yweather:forecast day="Sat" date="19 Dec 2009" low="49" high="65" text="Partly Cloudy" code="30" />
<guid isPermaLink="false">USCA1116_2009_12_18_9_38_PST</guid>
</item>
</chann
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.