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