-
Notifications
You must be signed in to change notification settings - Fork 1
/
frmRss.vb
200 lines (136 loc) · 5.57 KB
/
frmRss.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Imports System.Xml
Imports System.Xml.XPath
Public Class frmRss
' Current Path to RSS Feed
Private mRssUrl As String
''' <summary>
''' Set the RSS URL to an empty
''' string on form load
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
' Clear path to RSS Feed
mRssUrl = String.Empty
End Sub
''' <summary>
''' Pull up an RSS feed and load its headlines
''' and links into a treeview control
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub tsRssGo_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles tsRssGo.Click
Try
' set the file path member var
mRssUrl = Me.tsRssLocation.Text
' Clear the treeview.
tvwRss.Nodes.Clear()
' set the wait cursor
Me.Cursor = Cursors.WaitCursor
' create a new xml doc
Dim doc As New XmlDocument()
Try
' load the xml doc
doc.Load(mRssUrl)
' return the cursor
Me.Cursor = Cursors.Default
Catch ex1 As Exception
' return the cursor
Me.Cursor = Cursors.Default
' tell a story
MessageBox.Show(ex1.Message)
Return
End Try
' get an xpath navigator
Dim navigator As XPathNavigator = doc.CreateNavigator()
Try
' look for the path to the rss item titles navigate
' through the nodes to get all titles
Dim nodes As XPathNodeIterator = navigator.Select("/rss/channel/item/title")
While nodes.MoveNext
' clean up the text for display
Dim node As XPathNavigator = nodes.Current
Dim tmp As String = node.Value.Trim()
tmp = tmp.Replace(ControlChars.CrLf, "")
tmp = tmp.Replace(ControlChars.Lf, "")
tmp = tmp.Replace(ControlChars.Cr, "")
tmp = tmp.Replace(ControlChars.FormFeed, "")
tmp = tmp.Replace(ControlChars.NewLine, "")
' add a new treeview node for this
' news item title
tvwRss.Nodes.Add(tmp)
End While
' set a position counter
Dim position As Integer = 0
' Get the links from the RSS feed
Dim nodesLink As XPathNodeIterator = navigator.Select("/rss/channel/item/link")
While nodesLink.MoveNext
' clean up the link
Dim node As XPathNavigator = nodesLink.Current
Dim tmp As String = node.Value.Trim()
tmp = tmp.Replace(ControlChars.CrLf, "")
tmp = tmp.Replace(ControlChars.Lf, "")
tmp = tmp.Replace(ControlChars.Cr, "")
tmp = tmp.Replace(ControlChars.FormFeed, "")
tmp = tmp.Replace(ControlChars.NewLine, "")
' use the position counter
' to add a link child node
' to each news item title
tvwRss.Nodes(position).Nodes.Add(tmp)
' increment the position counter
position += 1
End While
Catch ex As Exception
MessageBox.Show(ex.Message, "RSS Feed Load Error")
End Try
' restore the cursor
Me.Cursor = Cursors.Default
Catch ex2 As Exception
' snitch
MessageBox.Show(ex2.ToString(), "RSS Feed Initialization Failure")
End Try
End Sub
''' <summary>
''' Upon selection of a node, open the link
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub tvwRss_AfterSelect(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.TreeViewEventArgs) _
Handles tvwRss.AfterSelect
Try
' get the first four characters from the link for a
' quick test
Dim tmp As String = tvwRss.SelectedNode.Text.Substring(0, 4)
' test the link text and then
' navigate the browser to that link
If tmp = "http" Then
webBrowser1.Navigate(tvwRss.SelectedNode.Text)
End If
Catch
' skip it if it isn't a link
End Try
End Sub
''' <summary>
''' Load the selected canned RSS feed into the
''' treeview using the existing methods
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub tsCboFeeds_SelectedIndexChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles tsCboFeeds.SelectedIndexChanged
' load the text for the selected feed into
' the RSS location text box and then use
' the existing button click event to launch
' the RSS feed into the treeview
tsRssLocation.Text = tsCboFeeds.SelectedItem.ToString()
tsRssGo_Click(Me, New EventArgs())
End Sub
End Class