用VB.NET写的一个简易的RSS阅读器

** 源代码如下: **

Imports System.Xml
Imports System.Threading

Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing ) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
Friend WithEvents Label4 As System.Windows.Forms.Label

  1<system.diagnostics.debuggerstepthrough()> Private  Sub  InitializeComponent()   
  2Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))   
  3Me.ListBox1 = New System.Windows.Forms.ListBox   
  4Me.Button1 = New System.Windows.Forms.Button   
  5Me.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser   
  6Me.Label1 = New System.Windows.Forms.Label   
  7Me.Label2 = New System.Windows.Forms.Label   
  8Me.TextBox1 = New System.Windows.Forms.TextBox   
  9Me.StatusBar1 = New System.Windows.Forms.StatusBar   
 10Me.Label4 = New System.Windows.Forms.Label   
 11CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).BeginInit()   
 12Me.SuspendLayout()   
 13'    
 14'ListBox1    
 15'    
 16Me.ListBox1.Location = New System.Drawing.Point(0, 67)   
 17Me.ListBox1.Name =  "ListBox1"    
 18Me.ListBox1.Size = New System.Drawing.Size(727, 108)   
 19Me.ListBox1.TabIndex = 0   
 20'    
 21'Button1    
 22'    
 23Me.Button1.Location = New System.Drawing.Point(633, 5)   
 24Me.Button1.Name =  "Button1"    
 25Me.Button1.Size = New System.Drawing.Size(94, 56)   
 26Me.Button1.TabIndex = 1   
 27Me.Button1.Text =  "读取"    
 28'    
 29'AxWebBrowser1    
 30'    
 31Me.AxWebBrowser1.Enabled =  True    
 32Me.AxWebBrowser1.Location = New System.Drawing.Point(0, 193)   
 33Me.AxWebBrowser1.OcxState = CType(resources.GetObject(  "AxWebBrowser1.OcxState"  ), System.Windows.Forms.AxHost.State)   
 34Me.AxWebBrowser1.Size = New System.Drawing.Size(727, 476)   
 35Me.AxWebBrowser1.TabIndex = 2   
 36'    
 37'Label1    
 38'    
 39Me.Label1.Location = New System.Drawing.Point(8, 26)   
 40Me.Label1.Name =  "Label1"    
 41Me.Label1.Size = New System.Drawing.Size(616, 15)   
 42Me.Label1.TabIndex = 3   
 43Me.Label1.Text =  "网站地址:"    
 44'    
 45'Label2    
 46'    
 47Me.Label2.Location = New System.Drawing.Point(8, 45)   
 48Me.Label2.Name =  "Label2"    
 49Me.Label2.Size = New System.Drawing.Size(616, 14)   
 50Me.Label2.TabIndex = 4   
 51Me.Label2.Text =  "网站描述:"    
 52'    
 53'TextBox1    
 54'    
 55Me.TextBox1.Location = New System.Drawing.Point(88, 0)   
 56Me.TextBox1.Name =  "TextBox1"    
 57Me.TextBox1.Size = New System.Drawing.Size(536, 20)   
 58Me.TextBox1.TabIndex = 6   
 59Me.TextBox1.Text =  "Http://Blog.CSDN.Net/AppleBBS/Rss.aspx"    
 60'    
 61'StatusBar1    
 62'    
 63Me.StatusBar1.Location = New System.Drawing.Point(0, 495)   
 64Me.StatusBar1.Name =  "StatusBar1"    
 65Me.StatusBar1.Size = New System.Drawing.Size(726, 22)   
 66Me.StatusBar1.TabIndex = 7   
 67Me.StatusBar1.Text =  "StatusBar1"    
 68'    
 69'Label4    
 70'    
 71Me.Label4.Location = New System.Drawing.Point(9, 6)   
 72Me.Label4.Name =  "Label4"    
 73Me.Label4.Size = New System.Drawing.Size(70, 12)   
 74Me.Label4.TabIndex = 8   
 75Me.Label4.Text =  "RSS地址:"    
 76Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft   
 77'    
 78'Form1    
 79'    
 80Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)   
 81Me.ClientSize = New System.Drawing.Size(726, 517)   
 82Me.Controls.Add(Me.Label4)   
 83Me.Controls.Add(Me.StatusBar1)   
 84Me.Controls.Add(Me.TextBox1)   
 85Me.Controls.Add(Me.Label2)   
 86Me.Controls.Add(Me.Label1)   
 87Me.Controls.Add(Me.AxWebBrowser1)   
 88Me.Controls.Add(Me.Button1)   
 89Me.Controls.Add(Me.ListBox1)   
 90Me.Name =  "Form1"    
 91Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen   
 92Me.Text =  "Easy RSS Reader"    
 93CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).EndInit()   
 94Me.ResumeLayout(  False  )   
 95  
 96End  Sub    
 97  
 98#  End  Region   
 99Private  thread As Thread  '定义一个线程    
100Private  Sub  loadrss()  '读取RSS文件并取出内容标题的过程    
101StatusBar1.Text =  "正在读取"  &amp; TextBox1.Text &amp; "并效验"    
102Me.loadxmltocache(TextBox1.Text)   
103StatusBar1.Text =  "正在读取相关网站信息"    
104Me.loadtitle()   
105StatusBar1.Text =  "正在读取相RSS内容项"    
106Me.loaditem()   
107StatusBar1.Text =  "完成"    
108End  Sub    
109  
110Private  Sub  Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
111Try   
112thread = New Thread(AddressOf loadrss)   
113thread.Start()   
114Catch ex As Exception   
115MsgBox(ex.ToString)   
116End  Try   
117End  Sub    
118  
119Private  Sub  Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   
120AxWebBrowser1.Width = Me.Width   
121StatusBar1.Text =  "就绪"    
122End  Sub    
123Private  Sub  loadxmltocache(ByVal URL As  String  )   
124'读取RSS文件并存放在本地以供操作    
125Dim xmldocument As New XmlDocument   
126xmldocument.Load(URL)   
127xmldocument.Save(Application.StartupPath &amp; "~doc.xml"  )   
128  
129End  Sub    
130Private  Sub  loadtitle()   
131'从本地文件中进行操作,读取RSS中有关网站的信息    
132Dim xmlDocument As New XmlDocument   
133xmlDocument.Load(Application.StartupPath &amp; "~doc.xml"  )   
134Dim mynodelist As XmlNodeList   
135mynodelist = xmlDocument.SelectNodes(  "/rss/channel"  )   
136Label1.Text =  "网站:"  &amp; Trim(mynodelist(0).Item(  "title"  ).InnerText())   
137Label2.Text =  "描述:"  &amp; Trim(mynodelist(0).Item(  "description"  ).InnerText())   
138End  Sub    
139Private  Sub  loaditem()   
140'从本地文件中进行操作,读取RSS中内容的标题及作者    
141Dim xmlDocument As New XmlDocument   
142xmlDocument.Load(Application.StartupPath &amp; "~doc.xml"  )   
143Dim mynodelist As XmlNodeList   
144mynodelist = xmlDocument.SelectNodes(  "/rss/channel/item"  )   
145ListBox1.Items.Clear()   
146Dim i As Integer   
147For  i = 0 To mynodelist.Count - 1   
148ListBox1.Items.Add(  "["  &amp; Trim(mynodelist(i).Item(  "dc:creator"  ).InnerText()) &amp; "]"  &amp; Trim(mynodelist(i).Item(  "title"  ).InnerText()))   
149Next    
150End  Sub    
151  
152Private  Sub  ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged   
153thread = New Thread(AddressOf loadhtml)   
154thread.Start()   
155End  Sub    
156Private  Sub  loadhtml()   
157'读取RSS中对应文件的内容并存到临时文件后显示    
158Dim xmlDocument As New XmlDocument   
159xmlDocument.Load(Application.StartupPath &amp; "~doc.xml"  )   
160Dim mynodelist As XmlNodeList   
161mynodelist = xmlDocument.SelectNodes(  "/rss/channel/item"  )   
162Dim i As Integer   
163Dim j As Boolean   
164Dim content As  String    
165StatusBar1.Text =  "正在读取文章内容"    
166For  i = 0 To mynodelist.Count - 1   
167If  ListBox1.SelectedItem =  "["  &amp; Trim(mynodelist(i).Item(  "dc:creator"  ).InnerText()) &amp; "]"  &amp; Trim(mynodelist(i).Item(  "title"  ).InnerText())  Then    
168content = content &amp; "<html><head><meta content="text/html;charset=utf-8" http-equiv="content-type"/></head><body>"    
169content = content &amp; "<table bgcolor="#eeeeee" height="80" width="100%"><tr><td><b>标题:</b><a href='"    
170content = content &amp; Trim(mynodelist(i).Item(  "link"  ).InnerText())   
171content = content &amp; "  ' target="_blank">"    
172content = content &amp; Trim(mynodelist(i).Item(  "title"  ).InnerText())   
173content = content &amp; "</a><br/><b>作者:</b>"    
174content = content &amp; Trim(mynodelist(i).Item(  "dc:creator"  ).InnerText())   
175content = content &amp; "</td></tr></table>"    
176content = content &amp; formatHtml(mynodelist(i).Item(  "description"  ).InnerText())   
177content = content &amp; "</body></html>"    
178j = SaveTextFile(Application.StartupPath &amp; "~temp.html"  , content)   
179AxWebBrowser1.Navigate(Application.StartupPath &amp; "~temp.html"  )   
180Exit  For    
181End  If    
182Next    
183StatusBar1.Text =  "完成"    
184End  Sub    
185'写文件的过程    
186Function  SaveTextFile(ByVal FilePath As  String  , ByVal FileContent As  String  ) As Boolean   
187Dim sw As System.IO.StreamWriter   
188Try   
189sw = New System.IO.StreamWriter(FilePath,  False  )   
190sw.Write(FileContent)   
191Return  True    
192Catch e As Exception   
193Return  False    
194Finally   
195If  Not sw Is  Nothing  Then  sw.Close()   
196End  Try   
197End  Function    
198'从地址里取网址的函数    
199Private  Function  formatURL(ByVal str As  String  ) As  String    
200Dim sStr As  String    
201Dim i As Integer   
202Dim j As Integer   
203For  i = 1 To Len(str)   
204sStr = sStr &amp; Mid  (str, i, 1)   
205If  Mid  (str, i, 1) =  "/"  Then    
206j += 1   
207End  If    
208If  j = 3  Then  Exit  For    
209Next    
210Return sStr   
211End  Function    
212'将内容里的相对图片地址改成绝对地址的函数    
213Private  Function  formatHtml(ByVal str As  String  ) As  String    
214Return Replace(str,  "src="  &amp; Chr(34) &amp; "/"  ,  "src ="  &amp; Chr(34) &amp; formatURL(TextBox1.Text))   
215End  Function    
216End  Class    
217  
218转自: http://www.cnblogs.com/aowind/archive/2005/03/16/119838.html</system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus