C#版簡易RSS閱讀器

來源:互聯網
上載者:User
C#版簡易RSS閱讀器。由VB版修改完成,感謝aowind的支援人員!


原始碼:
using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Xml;

using System.IO;

using System.Threading;

namespace YuLRSSReader

{

    /// <summary>

    /// Form1 的摘要說明。

    /// </summary>

    public class Form1 : System.Windows.Forms.Form

    {

        private System.Windows.Forms.Label label1;

        private System.Windows.Forms.Label label2;

        private System.Windows.Forms.Label label3;

        private System.Windows.Forms.TextBox textBox1;

        private System.Windows.Forms.Button button1;

        private System.Windows.Forms.Label BlogName;

        private System.Windows.Forms.Label BlogDescription;

        private System.Windows.Forms.ListBox listBox1;

        private System.Windows.Forms.StatusBar statusBar1;

        private AxSHDocVw.AxWebBrowser axWebBrowser1;

        /// <summary>

        /// 必需的設計器變數。

        /// </summary>

        private System.ComponentModel.Container components = null;

        public Form1()

        {

            //

            // Windows 表單設計器支援所必需的

            //

            InitializeComponent();

            //

            // TODO: 在 InitializeComponent 調用後添加任何建構函式代碼

            //

        }

        /// <summary>

        /// 清理所有正在使用的資源。

        /// </summary>

        protected override void Dispose( bool disposing )

        {

            if( disposing )

            {

                if (components != null)

                {

                    components.Dispose();

                }

            }

            base.Dispose( disposing );

        }

        #region Windows 表單設計器產生的程式碼

        /// <summary>

        /// 設計器支援所需的方法 - 不要使用代碼編輯器修改

        /// 此方法的內容。

        /// </summary>

        private void InitializeComponent()

        {

           
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(Form1));

            this.label1 = new System.Windows.Forms.Label();

            this.label2 = new System.Windows.Forms.Label();

            this.label3 = new System.Windows.Forms.Label();

            this.textBox1 = new System.Windows.Forms.TextBox();

            this.button1 = new System.Windows.Forms.Button();

            this.BlogName = new System.Windows.Forms.Label();

            this.BlogDescription = new System.Windows.Forms.Label();

            this.listBox1 = new System.Windows.Forms.ListBox();

            this.statusBar1 = new System.Windows.Forms.StatusBar();

            this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();

           
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();

            this.SuspendLayout();

            //

            // label1

            //

            this.label1.Location = new System.Drawing.Point(8, 8);

            this.label1.Name = "label1";

            this.label1.Size = new System.Drawing.Size(64, 23);

            this.label1.TabIndex = 0;

            this.label1.Text = "RSS地址:";

            //

            // label2

            //

            this.label2.Location = new System.Drawing.Point(8, 40);

            this.label2.Name = "label2";

            this.label2.Size = new System.Drawing.Size(64, 23);

            this.label2.TabIndex = 1;

            this.label2.Text = "BLOG:";

            //

            // label3

            //

            this.label3.Location = new System.Drawing.Point(8, 72);

            this.label3.Name = "label3";

            this.label3.Size = new System.Drawing.Size(64, 23);

            this.label3.TabIndex = 2;

            this.label3.Text = "描述:";

            //

            // textBox1

            //

            this.textBox1.Location = new System.Drawing.Point(72, 8);

            this.textBox1.Name = "textBox1";

            this.textBox1.Size = new System.Drawing.Size(392, 21);

            this.textBox1.TabIndex = 3;

            this.textBox1.Text = "";

            //

            // button1

            //

            this.button1.Location = new System.Drawing.Point(472, 16);

            this.button1.Name = "button1";

            this.button1.Size = new System.Drawing.Size(80, 64);

            this.button1.TabIndex = 4;

            this.button1.Text = "讀取";

            this.button1.Click += new System.EventHandler(this.button1_Click);

            //

            // BlogName

            //

            this.BlogName.Location = new System.Drawing.Point(72, 40);

            this.BlogName.Name = "BlogName";

            this.BlogName.Size = new System.Drawing.Size(392, 23);

            this.BlogName.TabIndex = 5;

            //

            // BlogDescription

            //

            this.BlogDescription.Location = new System.Drawing.Point(72, 72);

            this.BlogDescription.Name = "BlogDescription";

            this.BlogDescription.Size = new System.Drawing.Size(392, 23);

            this.BlogDescription.TabIndex = 6;

            //

            // listBox1

            //

            this.listBox1.ItemHeight = 12;

            this.listBox1.Location = new System.Drawing.Point(0, 104);

            this.listBox1.Name = "listBox1";

            this.listBox1.Size = new System.Drawing.Size(560, 112);

            this.listBox1.TabIndex = 7;

           
this.listBox1.SelectedIndexChanged += new
System.EventHandler(this.listBox1_SelectedIndexChanged);

            //

            // statusBar1

            //

            this.statusBar1.Location = new System.Drawing.Point(0, 591);

            this.statusBar1.Name = "statusBar1";

            this.statusBar1.Size = new System.Drawing.Size(562, 22);

            this.statusBar1.TabIndex = 8;

            //

            // axWebBrowser1

            //

            this.axWebBrowser1.Dock = System.Windows.Forms.DockStyle.Bottom;

            this.axWebBrowser1.Enabled = true;

            this.axWebBrowser1.Location = new System.Drawing.Point(0, 223);

           
this.axWebBrowser1.OcxState =
((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));

            this.axWebBrowser1.Size = new System.Drawing.Size(562, 368);

            this.axWebBrowser1.TabIndex = 9;

            //

            // Form1

            //

            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

            this.ClientSize = new System.Drawing.Size(562, 613);

            this.Controls.Add(this.axWebBrowser1);

            this.Controls.Add(this.statusBar1);

            this.Controls.Add(this.listBox1);

            this.Controls.Add(this.BlogDescription);

            this.Controls.Add(this.BlogName);

            this.Controls.Add(this.button1);

            this.Controls.Add(this.textBox1);

            this.Controls.Add(this.label3);

            this.Controls.Add(this.label2);

            this.Controls.Add(this.label1);

           
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;

            this.Name = "Form1";

           
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;

            this.Text = "YuL\'s RSS Reader";

            this.Load += new System.EventHandler(this.Form1_Load);

           
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();

            this.ResumeLayout(false);

        }

        #endregion

        /// <summary>

        /// 應用程式的主進入點。

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.Run(new Form1());

        }

        private void Form1_Load(object sender, System.EventArgs e)

        {

            this.axWebBrowser1.Width = this.Width;

            this.statusBar1.Text = "就緒";

        }

        private Thread thread;

        private string fileFullPath = Application.StartupPath + "~tempdoc.xml";

        private void LoadRss()

        {

            this.statusBar1.Text = "正在讀取" + this.textBox1.Text + "並校正…";

            this.LoadXml2Coach(this.textBox1.Text);

            this.statusBar1.Text = "正在讀取BLOG相關資訊…";

            this.LoadTitle();

            this.statusBar1.Text = "正在讀取RSS內容資訊…";

            this.LoadItem();

            this.statusBar1.Text = "完成";

        }

        private void LoadXml2Coach(String url)

        {

            XmlDocument doc = new XmlDocument();

            doc.Load(url);

            doc.Save(this.fileFullPath);

        }

        private void LoadTitle()

        {

            XmlDocument doc = new XmlDocument();

            doc.Load(this.fileFullPath);

            XmlNodeList nodeList;

            nodeList = doc.SelectNodes("/rss/channel");

            this.BlogName.Text = nodeList[0].SelectSingleNode("title").InnerText;

           
this.BlogDescription.Text =
nodeList[0].SelectSingleNode("description").InnerText;

        }

        private void LoadItem()

        {

            XmlDocument doc = new XmlDocument();

            doc.Load(this.fileFullPath);

            XmlNodeList nodeList;

            nodeList = doc.SelectNodes("/rss/channel/item");

            this.listBox1.Items.Clear();

            XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);

            ns.AddNamespace("dc","http://purl.org/dc/elements/1.1/");

            try

            {

                for(int i = 0; i < nodeList.Count; i++)

                {

           
        this.listBox1.Items.Add("[" +
nodeList[i].SelectSingleNode("dc:creator",ns).InnerText + "]" +
nodeList[i].SelectSingleNode("title").InnerText);

                }

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

        }

        private void LoadHtml()

        {

            XmlDocument doc = new XmlDocument();

            doc.Load(this.fileFullPath);

            XmlNodeList nodeList;

            nodeList = doc.SelectNodes("/rss/channel/item");

            bool flag;

            string temp = "";

            string content = "";

            this.statusBar1.Text = "正在讀取文章內容…";

            XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);

            ns.AddNamespace("dc","http://purl.org/dc/elements/1.1/");

            for(int i = 0; i < nodeList.Count; i++)

            {

           
    temp = "[" +
nodeList[i].SelectSingleNode("dc:creator",ns).InnerText + "]" +
nodeList[i].SelectSingleNode("title").InnerText;

           
   
if(this.listBox1.SelectedItem.ToString().Equals(temp))

                {

           
        content = content +
"<html><head><meta http-equiv='content-type'
content='text/html;charset=UTF-8'></head><body>";

           
        content = content + "<table
width='100%' height='80'
bgcolor='#eeeeee'><tr><td><b>標題:</b><a
href='";

           
        content = content +
nodeList[i].SelectSingleNode("link").InnerText;

           
        content = content + "'
target='_blank'>";

           
        content = content +
nodeList[i].SelectSingleNode("title").InnerText;

           
        content = content +
"</a><br><b>作者:</b>";

           
        content = content +
nodeList[i].SelectSingleNode("dc:creator",ns).InnerText;

           
        content = content +
"</tb></tr></table>";

           
        content = content +
nodeList[i].SelectSingleNode("description").InnerText;

           
        content = content +
"</body></html>";

           
        flag =
SaveTextFile(Application.StartupPath + "~temp.html", content);

                   

                    object flags = 0;

           
        object targetFrame =
String.Empty;

                    object postData = String.Empty;

                    object headers = String.Empty;

           
       
this.axWebBrowser1.Navigate(Application.StartupPath + "~temp.html", ref
flags, ref targetFrame, ref postData, ref headers);

                    break;

                }

            }

            this.statusBar1.Text = "完成";

        }

        private bool SaveTextFile(string filePath, string fileContent)

        {

            System.IO.StreamWriter sw = null;

            try

            {

                sw = new StreamWriter(filePath,false);

                sw.Write(fileContent);

                return true;

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.ToString());

                return false;

            }

            finally

            {

                if(sw != null)

                    sw.Close();

            }

        }

        private void button1_Click(object sender, System.EventArgs e)

        {

            try

            {

           
    this.thread = new Thread(new
ThreadStart(this.LoadRss));

                this.thread.Start();

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

        }

        private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)

        {

            try

            {

           
    this.thread = new Thread(new
ThreadStart(this.LoadHtml));

                this.thread.Start();

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

        }

    }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.