C # simple RSS reader. The modification was completed by the VB version. Thank you for your technical support!
Source code:
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>
/// Summary of 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>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;
Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();
//
// TODO: add Any constructor code after InitializeComponent calls
//
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </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 address :";
//
// 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 = "Description :";
//
// 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 = "read ";
This. button1.Click + = new System. EventHandler (this. button#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. listbox#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. form#load );
(System. ComponentModel. ISupportInitialize) (this. axWebBrowser1). EndInit ();
This. ResumeLayout (false );
}
# Endregion
/// <Summary>
/// Main entry point of the application.
/// </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 = "ready ";
}
Private Thread thread;
Private string fileFullPath = Application. StartupPath + "~ Tempdoc. xml ";
Private void LoadRss ()
{
This. statusBar1.Text = "reading" + this. textBox1.Text + "and verifying... ";
This. LoadXml2Coach (this. textBox1.Text );
This. statusBar1.Text = "reading BLOG information... ";
This. LoadTitle ();
This. statusBar1.Text = "reading RSS content... ";
This. LoadItem ();
This. statusBar1.Text = "finished ";
}
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 = "reading article content... ";
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> Content = 'text/html; charset = UTF-8 '>
Content = content + "<table
Width = '000000' height = '80'
Bgcolor = '# eeeeee'> <tr> <td> <B> title: </B> <
Href = '";
Content = content +
NodeList [I]. SelectSingleNode ("link"). InnerText;
Content = content + "'
Target = '_ blank'> ";
Content = content +
NodeList [I]. SelectSingleNode ("title"). InnerText;
Content = content +
"</A> <br> <B> author: </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>
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 = "finished ";
}
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 button#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 ());
}
}
}
}