嗨,我正在嘗試制作一個 C# 表單應用程式來讀取并顯示更新(例如每 5 分鐘)我當前的代碼以讀取 RSS 提要的 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 變數。如何顯示提要中的特定專案?
提前致謝
uj5u.com熱心網友回復:
將資料讀入串列。將計時器添加到您的表單。然后,將您的代碼放置在Timer.Tick事件處理程式中以顯示所需的資料。
嘗試以下操作:
與 2019 年相比:
- 創建 Windows 表單應用程式 (.NET Framework)
打開解決方案資源管理器:
- 在 VS 選單中,選擇解決方案資源管理器
將 Load 事件處理程式添加到 Form
- 在解決方案資源管理器中,右鍵單擊Form1.cs
- 選擇視圖設計器
- 在 Designer 中,雙擊 Form1 以將 Load 事件處理程式添加到表單
添加參考(System.ServiceModel)
- 在 VS 選單中,單擊專案
- 選擇添加參考...
- 選擇裝配體
- 檢查System.ServiceModel
創建類(名稱:FeedInfo.cs)
- 在 VS 選單中,單擊專案
- 選擇添加類...(名稱: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代碼
- 在解決方案資源管理器中,右鍵單擊Form1.cs
- 選擇查看代碼
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;
}
}
}
其他資源:
- SyndicationFeed.Load 方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/419180.html
標籤:
上一篇:為什么物件參考未設定為LINQ中物件的實體,其中組合框中的兩列
下一篇:XML反序列化C#
