• 大小: 49KB
    文件类型: .7z
    金币: 1
    下载: 0 次
    发布日期: 2021-06-07
  • 语言: C#
  • 标签: 新浪  新闻  RSS  C#  winform  

资源简介

实现读取新浪新闻的RSS源,并显示新闻内容,使用C#开发

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using RssReader.AppCode;
using SinaRssNews;

namespace RssReader
{
    public partial class MainForm : Form
    {
        private SinaNewsRss sinaNews = new SinaNewsRss();

        public MainForm()
        {
            InitializeComponent();

            // set browser
            webBrowser.scriptErrorsSuppressed = true;

            // load
            try
            {
                LoadNewsTree();
            }
            catch (Exception)
            {
                MessageBox.Show(@“初始化客户端失败,请重新打开!“);
                Application.Exit();
            }
        }

        private void webBrowser_ProgressChanged(object sender WebBrowserProgressChangedEventArgs e)
        {
            progressBarWebPage.Visible = true;
            if ((e.CurrentProgress > 0) && (e.MaximumProgress > 0))
            {
                progressBarWebPage.Maximum = Convert.ToInt32(e.MaximumProgress);
                progressBarWebPage.Step = Convert.ToInt32(e.CurrentProgress);
                progressBarWebPage.PerformStep();
            }
            else if (webBrowser.ReadyState == WebBrowserReadyState.Complete)
            {
                progressBarWebPage.Value = 0;
                progressBarWebPage.Visible = false;
            }
        }

        private void treeNews_AfterSelect(object sender TreeViewEventArgs e)
        {
            if (treeNews.SelectedNode.Tag != null)
            {
                LoadNewsItems(treeNews.SelectedNode.Tag.ToString());
            }
        }

        private void LoadNewsTree()
        {
            // clear first
            treeNews.Nodes.Clear();

            treeNews.Nodes.AddRange(sinaNews.sinaNewsTree.ToArray());
        }

        private void tableNewsItems_Selectionchanged(object sender EventArgs e)
        {
            try
            {
                LoadNews(tableNewsItems.CurrentRow.Cells[“Url“].Value.ToString());
            }
            catch (Exception)
            {

                MessageBox.Show(@“网络繁忙,请重试!“);
            }
        }

        private void LoadNewsItems(string url)
        {
            IList newsItems;

            try
            {
                newsItems = sinaNews.GetNewsItems(url);

                tableNewsItems.Rows.Clear();

                if (newsItems.Count == 0)
                {
                    MessageBox.Show(@“目前没有新闻,请刷新试试“);
                    return;
                }

                foreach (NewsItem newsItem in newsItems)
                {
                    DataGridViewRow row = new DataGridViewRow();

                    row.CreateCells(tableNewsItems newsItem.title newsItem.Description newsItem.link);

                    tableNewsItems.Rows.Add(row);
                }
            }
            catch (Exception)
            {
     

评论

共有 条评论