4

Hi I am trying to create a 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 I have come across are done in visual studio and it seems that I cant use this:

       (XmlReader reader = XmlReader.Create(Url))

This is what I have got so far. It doesn't 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 am not sure what I should do. Please help.

Sindu_
  • 1,347
  • 8
  • 27
  • 67

1 Answers1

2

Well your code is working as expected, since you are returning a List of RSSNews items, but you are assigning it to the textbox in a wrong way. Doing textBox1.Text = s.ToString(); will give System.Collections.Generic.List.... as a result.

Your method is reading RssNews items from the dataset and return around 23 items against the feed. You need to iterate through these items and display its text in the textbox, or better if you may use GridView or similar control to show these results.

You may try the following code in your 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 string for each item of RssNews and will display the result in textBox1.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • @Habib Which one is main method here? – envyM6 Oct 21 '14 at 20:06
  • @envyM6, This is old old old, but I guess Main would be the `Page_Load` event where the OP is trying to assign the result to `TextBox` – Habib Oct 21 '14 at 20:08
  • I know its old but im trying to understand this and struggling... so instead of `private void Form1_Load(object sender, EventArgs e) { var s = RssReader.Read("http://ph.news.yahoo.com/rss/philippines"); textBox1.Text = s.ToString(); }` It will be... – envyM6 Oct 21 '14 at 20:12
  • @envyM6, yes replace the code inside `Form1_Load` event with this code. – Habib Oct 21 '14 at 20:13
  • `private void Form1_Load(object sender, EventArgs e) { 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(); }` – envyM6 Oct 21 '14 at 20:13
  • @envyM6, looks fine, try in visual studio, – Habib Oct 21 '14 at 20:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63435/discussion-between-envym6-and-habib). – envyM6 Oct 21 '14 at 20:16