所以我正在开发一个抓取Rss并在Treeview中显示它的Windows窗体应用程序。所以一切都在运作,所以我的问题是当有新的Rss时我将如何显示通知。例如(如果有新的rss,notifyicon控件应通知用户并向他们显示有关该新Feed的信息)。下面的代码显示了我如何抓取RSS
public void MainRss()
{
try
{
//WFTvw is Treeview Name
// create a new xml doc
XmlDocument doc = new XmlDocument();
try
{
// load the xml doc
doc.Load(RssUrl);
}
catch (Exception ex1)
{
MessageBox.Show(ex1.Message);
return;
}
// get an xpath navigator
XPathNavigator navigator = doc.CreateNavigator();
try
{
// look for the path to the rss item titles; navigate
// through the nodes to get all titles
XPathNodeIterator nodes = navigator.Select("//rss/channel/item/title");
while (nodes.MoveNext())
{
// clean up the text for display
XPathNavigator node = nodes.Current;
string dspRss = node.Value.Trim();
dspRss = dspRss.Replace("\n", "");
dspRss = dspRss.Replace("\r", "");
// add a new treeview node for this
// news item title
WFTvw.Nodes.Add(tmp);
}
// set a position counter
int position = 0;
// Get the links from the RSS feed
XPathNodeIterator nodesLink = navigator.Select("//rss/channel/item/author");
while (nodesLink.MoveNext())
{
// clean up the link
XPathNavigator node = nodesLink.Current;
string dspRss2 = node.Value.Trim();
dspRss2 = dspRss2.Replace("\n", "");
dspRss2 = dspRss2.Replace("\r", "");
// clean up the link
// use the position counter
// to add a link child node
// to each news item title
WFTvw.Nodes[position].Nodes.Add(dspRss2);
// increment the position counter
position++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "RSS Feed Load Error");
}
// restore the cursor
this.Cursor = Cursors.Default;
WFTvw.ExpandAll();
}
catch (Exception ex2)
{
// snitch
MessageBox.Show(ex2.ToString(), "RSS Feed Initialization Failure");
}
}
以下是我如何更新我的RSS
public void refreshRssTimer()
{
var timer = new Timer();
timer.Tick += new EventHandler(refreshTimer_Tick);
timer.Interval = 60000; //60 seconds
timer.Start();
}
和计时器勾选
private void refreshTimer_Tick(object sender, EventArgs e)
{
WFTvw.Nodes.Clear();
MainRss();
}