C#读取rss feed并在设定的时间显示每一项

C# read rss feed and display each item at a set time

您好,我正在尝试制作一个 C# 表单应用程序来读取和显示更新的 RSS 提要(例如,每 5 分钟) 我当前读取 RSS 提要的代码如下

    public void ReadRSS()
    {
        string url = "https://www.theage.com.au/rss/feed.xml";
        XmlReader reader = XmlReader.Create(url);
        SyndicationFeed feed = SyndicationFeed.Load(reader);
        reader.Close();
        int RssItemCount = SyndicationFeed.Load(reader).Items.Count();
        while (CurrentItem == RssItemCount)
        {
            foreach (SyndicationItem item in feed.Items)
            {
                String subject = item.Title.Text;
                String summary = item.Summary.Text;
                label1.Text = subject;
                label2.Text = summary;
            }
        }
        CurrentItem++;
    }

我有一个计时器,我的想法是在每次滴答时转到提要中的下一个项目,因此是 CurrentItem 变量。我如何显示 Feed 中的特定项目?

提前致谢

将数据读入列表。添加 Timer to your form. Then, place your code to display the desired data inside the Timer.Tick 事件处理程序。

尝试以下操作:

VS 2019:

  • 创建 Windows 表单应用程序 (.NET Framework)

打开解决方案资源管理器:

  • 在 VS 菜单中,select 解决方案资源管理器

将 Load 事件处理程序添加到表单

  • 在解决方案资源管理器中,right-clickForm1.cs
  • Select 视图设计器
  • 在 Designer 中,double-clickForm1 将 Load 事件处理程序添加到表单

添加引用 (System.ServiceModel)

  • 在 VS 菜单中,单击 项目
  • Select 添加参考...
  • Select 程序集
  • 勾选System.ServiceModel

创建class(名称:FeedInfo.cs)

  • 在 VS 菜单中,单击 项目
  • Select 添加Class...(名称:FeedInfo.cs)

FeedInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReadRSS
{
    public class FeedInfo
    {
        public string Subject { get; set; }
        public string Summary { get; set; }
    }
}

修改Form1.cs代码

  • 在解决方案资源管理器中,right-clickForm1.cs
  • Select 查看代码

Form1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Diagnostics;

namespace ReadRSS
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
        private List<FeedInfo> feedInfos = new List<FeedInfo>();
        private int currentIndex = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //ToDo: set to desired value
            //timer1.Interval = 1000 * 60 * 5;
            timer1.Interval = 1000; //ms

            //subscribe to event (add event handler)
            timer1.Tick += Timer1_Tick;

            //get data
            feedInfos = GetRSSInfo();
            //Debug.WriteLine("feedInfos.Count: " + feedInfos.Count.ToString());

            //start timer
            timer1.Start();
        }

        private void Timer1_Tick(object sender, EventArgs e)
        {
            //ToDo: add desired code
            Debug.WriteLine("[{0}]: Subject: {1} Summary: {2}", currentIndex, feedInfos[currentIndex].Subject, feedInfos[currentIndex].Summary);

            if (currentIndex < feedInfos.Count - 1)
                currentIndex += 1; //increment
            else
                currentIndex = 0; //set value

        }

        private List<FeedInfo> GetRSSInfo()
        {
            List<FeedInfo> feedInfoList = new List<FeedInfo>();

            string url = "https://www.theage.com.au/rss/feed.xml";

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

                int RssItemCount = feed.Items.Count();

                foreach (SyndicationItem item in feed.Items)
                {
                    string subject = item.Title.Text;
                    string summary = item.Summary.Text;
                    //Debug.WriteLine("Subject: " + subject + " Summary: " + summary);

                    //create new instance
                    FeedInfo fInfo = new FeedInfo() { Subject = subject, Summary = summary };

                    //add
                    feedInfoList.Add(fInfo);
                }
            }

            return feedInfoList;
        }
    }
}

其他资源: