天天看點

C#版RSS閱讀器(新增讀OPML功能)

C#版RSS閱讀器(新增讀OPML功能)

C#版RSS閱讀器(新增讀OPML功能)
C#版RSS閱讀器(新增讀OPML功能)

新增一個treeview控件,用來顯示讀取的OPML結果,目前我所知道地僅部落格園有OPML提供。并且它的OPML還不是統一的格式,是以不得不在LoadOpml()方法裡添加一句判斷(藍色部分),不爽!呵呵……可能是還沒形成标準吧,也許将來就不用這麼麻煩了。

新增源代碼(部分):

        private void LoadOpml(string url)

        {

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

            this.LoadXml2Coach(this.textBox1.Text);

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

            this.LoadOpml();

            this.statusBar1.Text = "完成";

        }

        private void LoadOpml()

        {

            XmlDocument doc = new XmlDocument();

            doc.Load(this.rssFullPath);

            XmlNodeList nodeList;

            nodeList = doc.SelectNodes("/opml/body/outline");

            if(nodeList[0].HasChildNodes)

            {

                nodeList = doc.SelectNodes("/opml/body/outline/outline");

            }

            this.treeView1.Nodes.Clear();

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

            {

                this.treeView1.Nodes.Add(nodeList[i].Attributes.GetNamedItem("title").InnerText);

            }

        }

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

        {

            this.LoadOpml(this.textBox1.Text);

        }

        private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)

        {

            XmlDocument doc = new XmlDocument();

            doc.Load(this.rssFullPath);

            XmlNodeList nodeList;

            nodeList = doc.SelectNodes("/opml/body/outline");

            if(nodeList[0].HasChildNodes)

            {

                nodeList = doc.SelectNodes("/opml/body/outline/outline");

            }

            string url = "";

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

            {

                if(this.treeView1.SelectedNode.Text.Equals(nodeList[i].Attributes.GetNamedItem("title").InnerText))

                {

                    url = nodeList[i].Attributes.GetNamedItem("xmlUrl").InnerText;

                }

            }

            this.LoadRss(url);

        }

    }

        private void LoadRss(string url)

        {

            this.statusBar1.Text = "正在讀取" + url + "并校驗…";

            this.LoadXml2Coach(url);

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

            this.LoadTitle();

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

            this.LoadItem();

            this.statusBar1.Text = "完成";

        }

C#版簡易RSS閱讀器。由 VB版修改完成,感謝aowind的技術支援!

C#版RSS閱讀器(新增讀OPML功能)

源代碼:

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());

            }

        }

    }

}