简体   繁体   中英

Get data from a CDATA in a RSS

Using the next feed to get the weather time:

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

There's a part in the DOM where I don't know to get the data. At this case, I'm only interested to get the url of the image:

<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>

This is my current code in C#. What do I need to access to the url?

        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)
                     };

It looks like you need to take the text of the description element, and then parse that:

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;

You'd probably want some validation that there actually is an img element with a url attribute, of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM