Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Posted
Comments
Richard MacCutchan 4-May-20 3:20am    
If you cannot find the node then create a dummy reference so the rest of your code does not crash.
[no name] 4-May-20 7:22am    
Can you confirm that feedImage itself is valid?
Member 12111727 4-May-20 8:08am    
Yes, feedImage is valid. The error Im getting is
System.NullReferenceException: 'Object reference not set to an instance of an object.'


and the code that is throwing the error is
If feedImage.SelectSingleNode("url") IsNot Nothing Then
[no name] 4-May-20 8:13am    
But for me that means exactly that feedImage is NULL.
I would also check here with
If feedImage IsNot Nothing Then
Richard MacCutchan 4-May-20 9:34am    
That is because you are not checking your results when you try to get a node from the XML. You should never assume that the result of a method call will return what you are expecting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900