2
votes

I have the following RSS feed. I want to read the information inside a specific description tag. For example i want to get the information in the description tag when its title tag consists of the current day.I cant figure out how to do this. Please help

<item>
<title>Forecast for Saturday as of Jul. 14 5:30 AM IST</title> //If today is Saturday get information in description tag
<link>http://www.wunderground.com/global/stations/43466.html</link>    
  <description>
Thunderstorm. Low:26 &amp;deg; C.
  </description>
  <pubDate>Sat, 14 Jul 2012 00:00:00 GMT</pubDate>
  <guid isPermaLink="false">1342267200-1-night</guid>
</item>
<item>
<title>Forecast for Sunday as of Jul. 14 5:30 AM IST</title>
<link>http://www.wunderground.com/global/stations/43466.html</link>
  <description>
Chance of a Thunderstorm. High:30 &amp;deg; C.
  </description>
  <pubDate>Sat, 14 Jul 2012 00:00:00 GMT</pubDate>
  <guid isPermaLink="false">1342353600-2-day</guid>
 </item>

I was able to get the current day using:

string datenow = DateTime.Today.ToString("dddd / M / yyyy");
string[] words= datenow.Split(' ');
string day = words[0];

This is how i am reading the RSS feed:

 public class RssReader
    {
        public static List<RssNews> Read(string url)
        {
            var webClient = new WebClient();

            string result = webClient.DownloadString(url);

            XDocument document = XDocument.Parse(result);

            return (from descendant in document.Descendants("item")
                    select new RssNews()
                    {
                        Description = descendant.Element("description").Value,
                        Title = descendant.Element("title").Value,
                        PublicationDate = descendant.Element("pubDate").Value
                    }).ToList();
        }
    }
2

2 Answers

1
votes

As per your Requirement you just want to get description of current day .You have list of News feeds with Title ,description ..etc Now You can change your class (add a method in Your class ) if day is Current Day

public static string GetCurrentDayDescription(){

   var lst = Read("url");
   var resDescription = (from x in lst where x.Title.Contains(day) 
                          select x.Description).ToArray() ;
    return resDescription[0] ;
}
1
votes

You should be able to use XmlSerializer to deserialize the rss feed directly without manual mapping.. You will need to modify your RssNews object to map correctly, e.g.:

[XmlRoot(ElementName="item")]
public class RssNews
{
    [XmlElement(ElementName = "title")]
    public string Title { get; set; }

    [XmlElement(ElementName = "pubDate")]
    public string PublicationDate  { get; set; }

    [XmlElement(ElementName = "description")]
    public string Description { get; set; }

    [XmlElement(ElementName = "link")]
    public string Link { get; set; }

    [XmlElement(ElementName = "guid")]
    public string Description { get; set; }
}

Now you should be able to use deserializer:

    var feed = new List<RssNews>();
    using (var webClient = new WebClient())
    {

        string result = webClient.DownloadString(url);
        using (var stringReader = new StringReader(result))
        {
            var serializer = new XmlSerializer(feed.GetType());
            feed = (List<RssNews>)serializer.Deserialize(stringReader);
        }
    }
    return feed;