Search code examples
c#winformsxmlreaderrss-reader

Using XML Reader to get specific information from an RSS feed into a winform


I'm new to C# so I will try to explain this to the best of my abillity.

I'm creating a simple podcast-application in C# and trying to use an XML-reader to recieve an RSS feed and then putting that information into diffrent winforms.

I've worked out how to get the main information I want into a listBox. What I cannot figure out however, is to get another piece of information (the summary) from the RSS feed into a richTextBox by clicking a list item in the listBox.

The class I made to read the RSS feed.

List item

public class RSSreader
{
    public static List<Tuple<string, string>> rssRead(string url)
    {
        string subject = "";
        string summary = "";
        var x = "";
        var y = "";
        var count = 0;
        var list = new List<Tuple<string, string>>();
        try
        {



            XmlReader reader = XmlReader.Create(url);
            SyndicationFeed feed = SyndicationFeed.Load(reader);
            reader.Close();

            foreach (SyndicationItem item in feed.Items)
            {
                count++;
                subject = item.Title.Text;
                summary = item.Summary.Text;
                x += count + " " + subject + " ";
                list.Add(new Tuple<string, string>("Avsnitt " + count+ " " + subject, summary));

            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        //Datahandler.SavePodFeed(list);
        return list ;
    }
}

public class RSSreader
{
    public static List<Tuple<string, string>> rssRead(string url)
    {
        string subject = "";
        string summary = "";
        var x = "";
        var y = "";
        var count = 0;
        var list = new List<Tuple<string, string>>();
        try
        {


            //string urlX = "http://feeds.soundcloud.com/users/soundcloud:users:298230954/sounds.rss";
            XmlReader reader = XmlReader.Create(url);
            SyndicationFeed feed = SyndicationFeed.Load(reader);
            reader.Close();

            foreach (SyndicationItem item in feed.Items)
            {
                count++;
                subject = item.Title.Text;
                summary = item.Summary.Text;
                x += count + " " + subject + " ";
                list.Add(new Tuple<string, string>("Avsnitt " + count+ " " + subject, summary));

            }

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

        return list ;

    }
}

What im using to populate the listbox

private void button1_Click(object sender, EventArgs e)
{
    var list = RSSreader.rssRead(tbxURL.Text);
    foreach (Tuple<string, string> item in list)
    {
        listBox2.Items.Add(item.Item1);
    }
    listBox2.Items.Add(RSSreader.rssRead(tbxURL.Text));
}

My take on populating the richTextBox from the listBox with the summary onclick.

private void listBox2_MouseClick(object sender, MouseEventArgs e)
{
    var list = RSSreader.rssRead(tbxURL.Text);
    foreach (Tuple<string, string> item in list)
    {
        if (item != listBox2.SelectedItem)
        {
            richTextBox1.Text = item.Item2.ToString();
        }
    }
}

I don't get any errors but it's only populating the richTextBox with the same information no matter which element in the list I click.


Solution

  • Here are a few pointers:

    • Make a class where you store the information from RSS for each element (eg. MyPodcastEntry.cs with properties Index (count), Subject, Summary, ...)
    • Populate those classes in your rssRead and add them to a List
    • Add that list to the listbox instead of using Tuple<>
    • Override ToString in that class to adjust how they are displayed in the listbox

    This will help you keep and use the information from RSS. There is no need to run rssRead every time you click the listbox. Once you have read the RSS feed and put the data into your class then you can access this information any time.

    Now in your MouseClick event handler you can access the SelectedItem of the listbox, cast it to your class, and then access the properties that you previously assigned, to assign to the richtextbox text, no need for either rssRead or the loop.

    Optional stuff:

    Have fun with C#!