1

I want to parse yahoo weather rss feed xml in here : http://developer.yahoo.com/weather/

My Rss Item

public class YahooWeatherRssItem
{
    public string Title { get; set; }
    public string Link { get; set; }
    public string Description { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    // temp, wind, etc...
}

My Rss Manager

public static IEnumerable<YahooWeatherRssItem> GetYahooWeatherRssItems(string rssUrl)
{
    XDocument rssXml = XDocument.Load(rssUrl);

    var feeds = from feed in rssXml.Descendants("item")
                select new YahooWeatherRssItem
                {
                    I can get following values
                    Title = feed.Element("title").Value,
                    Link = feed.Element("link").Value,
                    Description = feed.Element("description").Value,

                    // I dont know, How can I parse these.
                    Text = ?
                    Temp = ?
                    Code = ?
                };
        return feeds;
    }

I dont know, How Can I parse following xml lines:

<yweather:condition  text="Mostly Cloudy"  code="28"  temp="50"  date="Fri, 18 Dec 2009 9:38 am PST" />
<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"/>

problem is yweather:<string>. There may be an article about xml parsing like this structure. Or code example?

Thanks.

AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
  • 1
    You need to access the Attribute's - see http://stackoverflow.com/questions/4429903/how-to-get-attribute-in-the-xdocument-object for an example. In your instance, `feed.Element("condition").Attribute("text").Value` for example. – dash Oct 18 '12 at 13:38
  • When I write `string value = xdoc.Root.Element("yweather:condition").Attribute("text").Value;` I got this error : `"The ':' character, hexadecimal value 0x3A, cannot be included in a name." string` – AliRıza Adıyahşi Oct 18 '12 at 13:43
  • When I write `feed.Element("condition").Attribute("text").Value` I m getting null object refrence error. I debugged it. I m Getting xml file with correct data. – AliRıza Adıyahşi Oct 18 '12 at 13:56

3 Answers3

2

The following expression should work, first reference the ycweather namespace;

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

then you get the attribute values this way:

Text = feed.Element(yWeatherNS + "condition").Attribute("text").Value

The problem is that your condition element in in another namespace, so you must select this node in the context of that namespace via the XNamespace.

You can read more about XML Namespaces via the MSDN article Namespaces in C#

dash
  • 89,546
  • 4
  • 51
  • 71
1

Use namespace and get the data using Attribute

XNamespace ns = "http://xml.weather.yahoo.com/ns/rss/1.0";
var feeds = from feed in rssXml.Descendants("item")
            select new YahooWeatherRssItem
            {

                Title = feed.Element("title").Value,
                Link = feed.Element("link").Value,
                Description = feed.Element("description").Value,
                Code=feed.Element(ns+"condition").Attribute("code").Value       
                //like above line, you can get other items 
            };

This will work . TESTED :)

Shyju
  • 214,206
  • 104
  • 411
  • 497
0

You need to read the XML attributes for those values. Based on the question here ( Find Elements by Attribute using XDocument ), you can try something like this:

Temp = feed.Attribute("temp");
Community
  • 1
  • 1
calmond
  • 189
  • 7