Rss Reader in Visual C # express edition

Hi, I am trying to create an RSS reader in Visual C # express. I need to read the rss feed into a text box when the form loads. I have never worked with RSS feeds before, and all the examples that I have met are executed in the visual studio, and it seems that I cannot use this:
(XmlReader reader = XmlReader.Create(Url)) 

This is what I got so far. This does not work.

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { var s = RssReader.Read("http://ph.news.yahoo.com/rss/philippines"); textBox1.Text = s.ToString(); } 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; } } 

I'm not sure what to do. Please, help.

+4
source share
1 answer

Well, your code works as expected, since you are returning a list of RSSNews items, but you are not assigning it to the text box correctly. Doing textBox1.Text = s.ToString(); will give System.Collections.Generic.List....

Your method reads RssNews elements from the dataset and returns about 23 elements against the feed. You need to iterate over these elements and display the text in the text box, or better if you can use a GridView or similar control to show these results.

You can try the following code in the Main method:

  var s = RssReader.Read("http://ph.news.yahoo.com/rss/philippines"); StringBuilder sb = new StringBuilder(); foreach (RssNews rs in s) { sb.AppendLine(rs.Title); sb.AppendLine(rs.PublicationDate); sb.AppendLine(rs.Description); } textBox1.Text = sb.ToString(); 

This will create a row for each RssNews element and display the result in textBox1.

+2
source

All Articles