2016-05-01 32 views
1

我正在開發一個Windows窗體應用程序C#,它將顯示新聞的標題。到目前爲止,我只能顯示第一個標題。我希望我的應用在20秒後顯示下一個標題等等。如果我用完標題,它會再次回到頂端。到目前爲止,我已經做到了這一點:如何在C#中的XML之後的一段時間後顯示不同的屬性?

private void GetNewsTopStories() 
{ 
    string queryNews = String.Format("http://news.yahoo.com/rss/"); 
    XmlDocument wData = new XmlDocument(); 
    wData.Load(queryNews); 

    XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable); 

    XmlNode channel = wData.SelectSingleNode("rss").SelectSingleNode("channel"); 
    XmlNodeList nodes = wData.SelectNodes("rss/channel/item", manager); 

    titleNews = channel.SelectSingleNode("item").SelectSingleNode("title").InnerText; 
    topNewsLabel.Text = titleNews.ToString(); 
} 

回答

0

試試這個

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Xml; 
using System.Xml.Linq; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     const string URL = "http://news.yahoo.com/rss/"; 
     string[] titles = null; 
     int count = -1; 
     public Form1() 
     { 
      InitializeComponent(); 
      timer1.Enabled = false; 
      timer1.Tick += new System.EventHandler(this.timer1_Tick); 
     } 

     private void buttonEnd_Click(object sender, EventArgs e) 
     { 
      timer1.Enabled = false; 
     } 

     private void buttonStart_Click(object sender, EventArgs e) 
     { 
      timer1.Interval = 20000; 
      count = -1; 
      timer1.Enabled = true; 

     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      if ((count == -1) || (count >= titles.Count())) 
      { 
       GetFeeds(); 
       count = 0; 
      } 
      textBox1.Text = titles[count++]; 

     } 
     public void GetFeeds() 
     { 
      timer1.Enabled = false; 
      XDocument doc = XDocument.Load(URL); 
      titles = doc.Descendants("item").Select(x => (string)x.Element("title")).ToArray(); 
      timer1.Enabled = true; 
     } 
    } 


} 
+0

謝謝你..這是一個很大的幫助! –

相關問題