I am trying to a read a RSS feed and display in my C# application. i have used the code below and it works perfectly for other RSS feeds. I want to read this RSS feed ---> http://ptwc.weather.gov/ptwc/feeds/ptwc_rss_indian.xml and the code below doesn't work for it. I don't get any errors but nothing happens, the text box which i want the RSS feed to be displayed is empty. Please help. What am I doing wrong?
public class RssNews
{
public string Title;
public string PublicationDate;
public string Description;
}
public class RssReader
{
public static List<RssNews> Read(string url)
{
var webResponse = WebRequest.Create(url).GetResponse();
if (webResponse == null)
return null;
var ds = new DataSet();
ds.ReadXml(webResponse.GetResponseStream());
var news = (from row in ds.Tables["item"].AsEnumerable()
select new RssNews
{
Title = row.Field<string>("title"),
PublicationDate = row.Field<string>("pubDate"),
Description = row.Field<string>("description")
}).ToList();
return news;
}
}
private string covertRss(string url)
{
var s = RssReader.Read(url);
StringBuilder sb = new StringBuilder();
foreach (RssNews rs in s)
{
sb.AppendLine(rs.Title);
sb.AppendLine(rs.PublicationDate);
sb.AppendLine(rs.Description);
}
return sb.ToString();
}
//Form Load code///
string readableRss;
readableRss = covertRss("http://ptwc.weather.gov/ptwc/feeds/ptwc_rss_indian.xml");
textBox5.Text = readableRss;
It seems that the DataSet.ReadXml method fails because there category is specified twice in the item, however under a different namespace.
This seems to work better:
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With