解决 在我的RSS馈线上显示超链接,缩略图和发布日期

Spixol.

新成员
加入
5月7日,2020年
消息
2
编程经验
Beginner
如何从以下RSS.xml Feed显示超链接,缩略图和发布日期: //www.economist.com/europe/rss.xml

我正在尝试从上面的饲料中拥有超链接,缩略图和发布日期。怎么走这个?

以下是我只显示标题和描述的代码:

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 { }
        }
    }
}
 
最后编辑了主持人:

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,903
地点
切萨皮克,va.
编程经验
10+
什么's wrong with just adding more FD_list.Items? Is it because FD_Item does not contain the things you want to display?
 

sh

众所周知的成员
加入
2018年9月5日
消息
1,982
编程经验
10+
由于您有一个问题,因为他们的RSS饲料没有提供拇指。您需要另一种方法来通过每个帖子进行,并在上传到文章的每个图像中刮除RSS Feed中的每个URI的标题图像。 (见截图1) - 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("//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("//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: 截屏
屏幕截图2: 截屏
 

Spixol.

新成员
加入
5月7日,2020年
消息
2
编程经验
Beginner
What's wrong with just adding more FD_list.Items? Is it because FD_Item does not contain the things you want to display?
你好,

谢谢 @skydiver说实话,我只是非常新的。正如我刚才弄明白的那样 @sheepings. 缩略图不会在我的RSS.xml Feed中显示,我会从这里说,我只想显示从RSS.xml Feed提供的“Link”和'发布'日期,但不是充满信用,可以使用'fd_item'命令来检索此类事物。你能在这方面帮忙吗?

亲切的问候,
Spixol.
由于您有一个问题,因为他们的RSS饲料没有提供拇指。您需要另一种方法来通过每个帖子进行,并在上传到文章的每个图像中刮除RSS Feed中的每个URI的标题图像。 (见截图1) - 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("//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("//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: 截屏
屏幕截图2: 截屏

谢谢 @sheepings. 我真的很欣赏你经历的努力,尽管在对你诚实的诚实上,我只是为了编程而新的,只有到目前为止获得基本概念/限制。所以我只想显示RSS.xml Feed中的“链接”和“发布日期”。你能帮忙吗?

亲切的问候,
Spixol.
 

跳伞运动员

工作人员
加入
2019年4月6日
消息
2,903
地点
切萨皮克,va.
编程经验
10+
如果您花时间挖掘您正在使用的各种对象/类的文档,它将有助于您的学习。只是模仿在线或教程中找到的代码只会让你到目前为止。

要获得发布日期,只需访问 发布属性.

联合项目可能有许多链接。看看 链接物业. 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.)
 

sh

众所周知的成员
加入
2018年9月5日
消息
1,982
编程经验
10+
我只想展示“链接”和“发布”日期
您只需要删除函数和其他一些线条。试试这个 :
C#:
        internal static void Execute_Response()
        {
            using XmlReader reader = XmlReader.Create("//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 }");
            }
        }
希望能帮助到你。
 
最佳 底部