Resolved Display Hyperlink, Thumbnail and Publish Date on my RSS Feeder

Spixol

New member
Joined
May 7, 2020
Messages
2
Programming Experience
Beginner
How to display the Hyperlink, Thumbnail, and Publish Date from the following RSS.XML Feed: https://www.economist.com/europe/rss.xml

I am trying to have the Hyperlink, Thumbnail and Publish Date from the feed above. How to go about this?

Below is my code that is displaying only the Title and Description:

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
using System.ServiceModel.Syndication;

namespace RSS_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                XmlReader FD_readxml = XmlReader.Create(textBox1.Text);
                SyndicationFeed FD_feed = SyndicationFeed.Load(FD_readxml);
                TabPage FD_tab = new TabPage(FD_feed.Title.Text);
                tabControl1.TabPages.Add(FD_tab);
                ListBox FD_list = new ListBox();
                FD_tab.Controls.Add(FD_list);
                FD_list.Dock = DockStyle.Fill;
                FD_list.HorizontalScrollbar = true;
                foreach (SyndicationItem FD_item in FD_feed.Items)
                {
                    FD_list.Items.Add(FD_item.Title.Text);
                    FD_list.Items.Add(FD_item.Summary.Text);
                    FD_list.Items.Add("---------");
                }
            }
            catch { }
        }
    }
}
 
Last edited by a moderator:
What's wrong with just adding more FD_list.Items? Is it because FD_Item does not contain the things you want to display?
 
Since you have an issue with the fact that their RSS feed doesn't provide thumbs. You will need another way to go through each of the posts and scrape the header image of each Uri in the RSS feed for each image uploaded to the articles. (See screenshot1) - That's what I done. You can then acquire each image from each post by passing the Uri's in the RSS to the Get_MetaImage(string Uri) function. The first method does all the leg work, and while the second function is called from the first method, it subsequently returns each image form each of the Uri taken from the RSS feed. And it does that by passing the Uri to the second function to request a return value.
C#:
        internal static void Request_Response()
        {
            using XmlReader reader = XmlReader.Create("https://www.economist.com/europe/rss.xml");
            /* Using Linq we will go over each of the Xml items. */
            foreach ((string item_Title, string item_Discription, DateTimeOffset item_Published, Uri item_Link) in
                from SyndicationItem item in SyndicationFeed.Load(reader).Items
                let item_Title = item.Title.Text
                let item_Discription = item.Summary.Text
                let item_Published = item.PublishDate
                let item_Link = item.Links[0]
                select (item_Title, item_Discription, item_Published, item_Link.Uri))
            {
                /* When an item is selected, this section executes per each item */
                Console.WriteLine($"Title - { item_Title }");
                Console.WriteLine($"Description - { item_Discription }");
                Console.WriteLine($"Date - { item_Published }");
                Console.WriteLine($"Link - { item_Link.AbsoluteUri }");
                /* Use the Get_MetaImage method to return the image per each Uri */
                Console.WriteLine($"Thumb - { Get_MetaImage(item_Link.AbsoluteUri) }");
                /* The thumb will need resizing. This can be done in an picturebox or alike. */
            }
        }
        public static string Get_MetaImage(string Uri)
        {
            WebRequest webRequest = WebRequest.Create(Uri); /* Create the webrequest and pass in the URI passes to this method. */
            string html; string imageSubStr; string imageSubStr_Result = string.Empty;
            try
            {
                using (StreamReader reader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
                {
                    /* Once we get a result, we will substring the value of the html and extract the image from the Uri source code. */
                    html = reader.ReadToEnd();
                    if (reader.EndOfStream.Equals(true))
                    {
                        imageSubStr = html.Substring(html.IndexOf("https://www.economist.com/sites/default/files/images/print-edition/"));
                        imageSubStr_Result = imageSubStr.Substring(0, imageSubStr.IndexOf($"\""));
                    }
                }
            }
            catch (WebException err)
            {
                if (err.Response != null && err.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse exResponse = (HttpWebResponse)err.Response;
                    if (exResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        /* Handle what to do if no image was used or uploaded on the page */
                    }
                }
                else
                {
                    /* Handle some other error responses */
                }
            }
            return imageSubStr_Result; /* Contains image of Uri */
        }
Simple stuff really. I advise running this code on new threads, and implement some delegation for talking to your UI. I've posted a lot of examples on how to do this if you search the forums, and iterate through my posts. I also recommend you extend the code I have provided you with and include a List<Tuple<string, string, string, string, string>> and for each link capable of returning all values from the RSS and scraping method. And add them to this list, and then update your Uri from that list. You should also go over the webexception catch block to ensure I haven't missed anything. Screenshot 2 shows a working example. Hope this helps, and hopefully you understand how i explained it, as I am rushing out at the minute.

Screenshot1 : Screenshot
Screenshot2 : Screenshot
 
What's wrong with just adding more FD_list.Items? Is it because FD_Item does not contain the things you want to display?
Hi,

Thanks @Skydiver, to be honest I am only very new. As I have just figured out by @Sheepings that the Thumbnail doesn't display in my RSS.XML feed, I would say from here that I'd only like to display the 'Link' and the 'Publish' Date that is provided from the RSS.XML feed, but not as confident enough to use the 'FD_Item' command to retrieve such things. Could you kindly help in this regard?

Kind regards,
Spixol
Since you have an issue with the fact that their RSS feed doesn't provide thumbs. You will need another way to go through each of the posts and scrape the header image of each Uri in the RSS feed for each image uploaded to the articles. (See screenshot1) - That's what I done. You can then acquire each image from each post by passing the Uri's in the RSS to the Get_MetaImage(string Uri) function. The first method does all the leg work, and while the second function is called from the first method, it subsequently returns each image form each of the Uri taken from the RSS feed. And it does that by passing the Uri to the second function to request a return value.
C#:
        internal static void Request_Response()
        {
            using XmlReader reader = XmlReader.Create("https://www.economist.com/europe/rss.xml");
            /* Using Linq we will go over each of the Xml items. */
            foreach ((string item_Title, string item_Discription, DateTimeOffset item_Published, Uri item_Link) in
                from SyndicationItem item in SyndicationFeed.Load(reader).Items
                let item_Title = item.Title.Text
                let item_Discription = item.Summary.Text
                let item_Published = item.PublishDate
                let item_Link = item.Links[0]
                select (item_Title, item_Discription, item_Published, item_Link.Uri))
            {
                /* When an item is selected, this section executes per each item */
                Console.WriteLine($"Title - { item_Title }");
                Console.WriteLine($"Description - { item_Discription }");
                Console.WriteLine($"Date - { item_Published }");
                Console.WriteLine($"Link - { item_Link.AbsoluteUri }");
                /* Use the Get_MetaImage method to return the image per each Uri */
                Console.WriteLine($"Thumb - { Get_MetaImage(item_Link.AbsoluteUri) }");
                /* The thumb will need resizing. This can be done in an picturebox or alike. */
            }
        }
        public static string Get_MetaImage(string Uri)
        {
            WebRequest webRequest = WebRequest.Create(Uri); /* Create the webrequest and pass in the URI passes to this method. */
            string html; string imageSubStr; string imageSubStr_Result = string.Empty;
            try
            {
                using (StreamReader reader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
                {
                    /* Once we get a result, we will substring the value of the html and extract the image from the Uri source code. */
                    html = reader.ReadToEnd();
                    if (reader.EndOfStream.Equals(true))
                    {
                        imageSubStr = html.Substring(html.IndexOf("https://www.economist.com/sites/default/files/images/print-edition/"));
                        imageSubStr_Result = imageSubStr.Substring(0, imageSubStr.IndexOf($"\""));
                    }
                }
            }
            catch (WebException err)
            {
                if (err.Response != null && err.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse exResponse = (HttpWebResponse)err.Response;
                    if (exResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        /* Handle what to do if no image was used or uploaded on the page */
                    }
                }
                else
                {
                    /* Handle some other error responses */
                }
            }
            return imageSubStr_Result; /* Contains image of Uri */
        }
Simple stuff really. I advise running this code on new threads, and implement some delegation for talking to your UI. I've posted a lot of examples on how to do this if you search the forums, and iterate through my posts. I also recommend you extend the code I have provided you with and include a List<Tuple<string, string, string, string, string>> and for each link capable of returning all values from the RSS and scraping method. And add them to this list, and then update your Uri from that list. You should also go over the webexception catch block to ensure I haven't missed anything. Screenshot 2 shows a working example. Hope this helps, and hopefully you understand how i explained it, as I am rushing out at the minute.

Screenshot1 : Screenshot
Screenshot2 : Screenshot

Thanks @Sheepings I really do appreciate the effort you went through, though in being honest with you I am just new to programming and only get basic concepts/limitations so far. So I would only like to display the 'Link' and 'Publish Date' that is in the RSS.XML feed. Can you help with this?

Kind regards,
Spixol
 
It would help your learning dramatically if you take time to dig into the documentation of the various objects/classes you are using. Just mimicking code found online or in tutorials will only get you so far.

To get the Publish date, just access the PublishDate property.

A syndication item may have many links. See the Links property. If you want the first one, you can use the First() LINQ extension method, or use the [0] indexing property of the Collection<SyndicationLink>. (The latter is more commonly used in the sample code written back in the early 2010's because LINQ was still in its infancy and had tenuous adoption.)
 
I'd only like to display the 'Link' and the 'Publish' Date
You only needed to remove the function and a few other lines. Try this :
C#:
        internal static void Execute_Response()
        {
            using XmlReader reader = XmlReader.Create("https://www.economist.com/europe/rss.xml");
            /* Using Linq we will go over each of the Xml items. */
            foreach ((DateTimeOffset item_Published, Uri item_Link) in
                from SyndicationItem item in SyndicationFeed.Load(reader).Items
                let item_Published = item.PublishDate
                let item_Link = item.Links[0]
                select (item_Published, item_Link.Uri))
            {
                /* When an item is selected, this section executes per each item */
                Console.WriteLine($"Date - { item_Published }");
                Console.WriteLine($"Link - { item_Link.AbsoluteUri }");
            }
        }
Hope it helps.
 
Back
Top Bottom