Im building a Natural Disaster tracker using RSS feed from various agencies. Most of the RSS feed structures are the same, but I have one feed from NASA that is slightly different than the rest. The others have an image node for the Feed Provider agency logo URL, which works fine, but the NASA one does not have an ImageURL node for their logo. I use a single Function to basically retrieve RSS data and fill a DataGridView and Form elements(labels and pictureboxes) with that data. I threw the code that looks for an image url node into a TRY/CATCH but it still throws an error because It cant find the imageURL node.
My Function to Retrieve and Fill data
<pre>Public Function SelectFeedToDisplay(FeedList As ComboBox, DataGrid As DataGridView, ProviderName As Label, ProviderImage As PictureBox, ProviderDesc As Label, ProviderLink As LinkLabel, FeedBuildDate As Label, ArticleLink As LinkLabel, ArticleTitle As Label, ArticleDate As Label, ArticleText As Label)
'RSS Connect
Dim url As String = FeedList.SelectedItem
Dim rssFeed As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
Dim response = rssFeed.GetResponse()
Dim rssStream = response.GetResponseStream()
Dim rssDoc As New XmlDocument()
rssDoc.Load(rssStream)
'Feed Provdier Basics
Dim feedImage As XmlNode = rssDoc.SelectSingleNode("rss/channel/image")
Dim feedTitle As XmlNode = rssDoc.SelectSingleNode("rss/channel/title")
Dim feedDesc As XmlNode = rssDoc.SelectSingleNode("rss/channel/description")
Dim feedLink As XmlNode = rssDoc.SelectSingleNode("rss/channel/link")
Dim lastBuildDate As XmlNode = rssDoc.SelectSingleNode("rss/channel/lastBuildDate")
'Feed Items
Dim rssItems As XmlNodeList = rssDoc.SelectNodes("rss/channel/item")
Dim i As Integer = 0
Dim dt As DataTable = New DataTable("table")
dt.Columns.Add("Title", Type.GetType("System.String"))
dt.Columns.Add("Date", Type.GetType("System.String"))
dt.Columns.Add("Text", Type.GetType("System.String"))
dt.Columns.Add("Link", Type.GetType("System.String"))
'Express Item Data
While i < rssItems.Count
Try
Dim node As XmlNode = rssItems.Item(i).SelectSingleNode("title")
If feedImage.SelectSingleNode("url") IsNot Nothing Then
Dim feedPic As XmlNode = feedImage.SelectSingleNode("url")
If feedPic IsNot Nothing Then
Dim imageURL As String = feedPic.InnerText
ProviderImage.Load(imageURL)
Else
feedImage = Nothing
End If
End If
Dim title As String
Dim link As String
Dim pubDate As String
Dim desc As String
'Feed Provider
If feedTitle IsNot Nothing Then
ProviderName.Text = feedTitle.InnerText
Else
ProviderName.Text = Nothing
End If
If lastBuildDate IsNot Nothing Then
FeedBuildDate.Text = lastBuildDate.InnerText
Else
FeedBuildDate.Text = Nothing
End If
If feedDesc IsNot Nothing Then
ProviderDesc.Text = feedDesc.InnerText
Else
ProviderDesc.Text = Nothing
End If
If feedLink IsNot Nothing Then
ProviderLink.Tag = feedLink.InnerText
Else
ProviderLink.Tag = Nothing
End If
'Item Nodes
If node IsNot Nothing Then
title = node.InnerText
Else
title = ""
End If
node = rssItems.Item(i).SelectSingleNode("pubDate")
If node IsNot Nothing Then
pubDate = node.InnerText
Else
pubDate = ""
End If
node = rssItems.Item(i).SelectSingleNode("link")
If node IsNot Nothing Then
link = node.InnerText
Else
link = Nothing
End If
node = rssItems.Item(i).SelectSingleNode("description")
If node IsNot Nothing Then
desc = node.InnerText
Else
desc = ""
End If
Dim dr As DataRow = dt.NewRow()
dr("Title") = title
dr("Date") = pubDate
dr("Text") = desc
dr("Link") = link
dt.Rows.Add(dr)
i += 1
Catch ex As Exception
End Try
End While
DataGrid.DataSource = dt
Return Nothing
End Function
The problem lies within the IF Statement inside the TRY statement:
If feedImage.SelectSingleNode("url") IsNot Nothing Then
Dim feedPic As XmlNode = feedImage.SelectSingleNode("url")
If feedPic IsNot Nothing Then
Dim imageURL As String = feedPic.InnerText
ProviderImage.Load(imageURL)
Else
feedImage = Nothing
End If
End If
What would be the best and effective way to check to see if a certain node exists before trying to retrieve info from it? Id like to implement it for all nodes, as the user of the application will be able to upload their own RSS feeds and if they differ from the ones provided, i have a feeling there will be worse problems than this.
What I have tried:
try Catch statements
If isNot statements
Google