0

使用下一个提要获取天气时间:

http://weather.yahooapis.com/forecastrss?w=2502265

DOM 中有一部分我不知道如何获取数据。在这种情况下,我只对获取图像的 url 感兴趣:

<description>
<![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/33.gif"/><br /> <b>Current Conditions:</b><br /> Fair, 70 F<BR /> <BR /><b>Forecast:</b><BR /> Wed - Mostly Cloudy. High: 82 Low: 66<br /> Thu - Sunny. High: 86 Low: 63<br /> Fri - Partly Cloudy. High: 74 Low: 59<br /> Sat - Partly Cloudy. High: 76 Low: 59<br /> Sun - Sunny. High: 77 Low: 58<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>

这是我当前的 C# 代码。我需要什么才能访问该网址?

        XNamespace yweather_NS = "http://xml.weather.yahoo.com/ns/rss/1.0";

        var query2 = from w in doc2.Descendants("channel")
                     select new WeatherDay()
                     {
                         Location = w.Element(yweather_NS + "location").Attribute("city").Value,
                         Temp = int.Parse(w.Element("item").Element(yweather_NS + "condition").Attribute("temp").Value),
                         Humidity = int.Parse(w.Element(yweather_NS + "atmosphere").Attribute("humidity").Value),
                         //Visibility = double.Parse(w.Element(yweather_NS + "atmosphere").Attribute("visibility").Value),
                         Condition = w.Element("item").Element(yweather_NS + "condition").Attribute("text").Value,
                         Pressure = double.Parse(w.Element(yweather_NS + "atmosphere").Attribute("pressure").Value)
                     };
4

1 回答 1

1

看起来您需要获取description元素的文本,然后对其进行解析:

var descriptionText = w.Element(yweather_NS + "description");
// The XML is a sequence of elements with no root: we'll add a root ourselves.
var descriptionXml = "<description>" + descriptionText.Trim() + "</description>";
var descriptionElement = XElement.Parse(description);
string url = descriptionElement.Element("img").Attribute("url").Value;

当然,您可能需要验证确实存在具有img属性url的元素。

于 2013-07-04T06:01:58.913 回答