How to display information from an RSS feed into a Label

Locked How to display information from an RSS feed into a Label

  • Tuesday, September 11, 2012 3:24 PM
     
      Has Code

    OK so my project is going great guns and I have refined the solution to a point i am very happy with.

    My only stumbling point seems to be the display of the information from the RSS feed.

    Here is my code as it stands.

        Private Sub Footer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'FOR THE RSS
            Dim rssURL As String = "G:\EVERYONE\INSCRO messages\BBC Top Stories.xml"
            Dim myRequest As System.Net.WebRequest = System.Net.WebRequest.Create(rssURL)
            Dim myResponse As System.Net.WebResponse = myRequest.GetResponse()
            Dim rssStream As System.IO.Stream = myResponse.GetResponseStream()
            Dim rssDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
            rssDoc.Load(rssStream)
            Dim rssItems As System.Xml.XmlNodeList = rssDoc.SelectNodes("rss/channel/item")
            Dim RSStitle As String = ""
            Dim RSSlink As String = ""
            Dim RSSdescription As String = ""
            Dim currentRssItem As System.Xml.XmlNode
            For Each currentRssItem In rssItems
                Dim rssDetail As System.Xml.XmlNode
                'RSStitle
                rssDetail = currentRssItem.SelectSingleNode("title")
                If Not IsNothing(rssDetail) Then
                    RSStitle = rssDetail.InnerText
                Else
                    RSStitle = "TITLE FAILED TO BE RETRIEVED"
                End If
                'RSSdescription
                rssDetail = currentRssItem.SelectSingleNode("description")
                If Not IsNothing(rssDetail) Then
                    RSSdescription = rssDetail.InnerText
                Else
                    RSSdescription = "DESCRIPTION FAILED TO BE RETRIEVED"
                End If
                RSSFeed.Text = RSStitle + " - " + RSSdescription
                Next
        End Sub

    The stumbling block is that the label will only populate for the last entry. I amthinking of using a loop but am at a loss as to which type would be best to suit purpose as i cant identify the number of entries.

    Any ideas are greatly appreciated.


    Peter

All Replies

  • Tuesday, September 11, 2012 4:48 PM
     
     

    I think the reason you see only the last entry is because the Text property of a label is a single String.

    So, each time you traverse the loop, this statement:

    RSSFeed.Text = RSStitle + " - " + RSSdescription

    *replaces* any previous string with the newest one.

    You can overcome the issue either by:

    1. Concatenating the strings in the label:

    RSSFeed.Text = RSSFeed.Text & " " & RSStitle + " - " + RSSdescription

    2. Or, by replacing the label with a control designed to hold multiple items, like a ListBox.

  • Wednesday, September 12, 2012 8:33 AM
     
     

    Thanks for that.

    Option 2 really isnt suitable for the solution as i need the feed to be displayed on one line so i need a loop to add to the string on each pass.

    Option 1 is more what i had in mind but when i have tried this it runs dog slow and I've tried to build on this but have had no joy with refining the solution.

    In my mind i have the solution as getting the number of items in the stream and then setting the loop to make that many passes. Then on every loop around add the information to the string.

    So:

    i = Count RSS News Items

    For 0 to i Loop

    RSSFeed.Text = RSS News Item + RSS News Item i

    I know this is not proper code but I'm just trying to illustrate my thought process. If it doenst help please just ignore it.


    Peter

  • Wednesday, September 12, 2012 12:29 PM
     
     

    That's what your current loop does.

    For Each currentRssItem In rssItems

    This for loop iterates once for each item in the stream.

    "when i have tried this it runs dog slow"

    Are you saying that concatening the strings slows your program down? Please show the exact code.

    Also, do you have a feel for the typical number of items in the stream? You might want to add a counter to your loop, then observe the result after the loop ends.
    • Edited by pvdg42MVP Wednesday, September 12, 2012 12:31 PM
    •  
  • Wednesday, September 12, 2012 1:03 PM
     
      Has Code

    OK this is what i did that made it slow:

        Private Sub Footer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'FOR THE RSS
            Dim rssURL As String = "G:\EVERYONE\INSCRO messages\BBC Top Stories.xml"
            Dim myRequest As System.Net.WebRequest = System.Net.WebRequest.Create(rssURL)
            Dim myResponse As System.Net.WebResponse = myRequest.GetResponse()
            Dim rssStream As System.IO.Stream = myResponse.GetResponseStream()
            Dim rssDoc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
            rssDoc.Load(rssStream)
            Dim rssItems As System.Xml.XmlNodeList = rssDoc.SelectNodes("rss/channel/item")
            Dim RSStitle As String = ""
            Dim RSSlink As String = ""
            Dim RSSdescription As String = ""
            Dim currentRssItem As System.Xml.XmlNode
            For Each currentRssItem In rssItems
                Dim rssDetail As System.Xml.XmlNode
                'RSStitle
                rssDetail = currentRssItem.SelectSingleNode("title")
                If Not IsNothing(rssDetail) Then
                    RSStitle = rssDetail.InnerText
                Else
                    RSStitle = "TITLE FAILED TO BE RETRIEVED"
                End If
                'RSSdescription
                rssDetail = currentRssItem.SelectSingleNode("description")
                If Not IsNothing(rssDetail) Then
                    RSSdescription = rssDetail.InnerText
                Else
                    RSSdescription = "DESCRIPTION FAILED TO BE RETRIEVED"
                End If
                
                RSSFeed.Text = RSSFeed.Text & " " & RSStitle + " - " + RSSdescription
                Next
    Any other attempts around this didnt even get an entry displayed.

    Peter

  • Thursday, September 13, 2012 12:57 PM
     
     Answered Has Code

    I see nothing in your code that jumps out as a culprit.

    One things you can change:

    Dim rssDetail As System.Xml.XmlNode
    Dim ItemCount as Integer = 0
    For Each currentRssItem In rssItems
                'Dim rssDetail As System.Xml.XmlNode
                'RSStitle
                rssDetail = currentRssItem.SelectSingleNode("title")
                If Not IsNothing(rssDetail) Then
                    RSStitle = rssDetail.InnerText
                Else
                    RSStitle = "TITLE FAILED TO BE RETRIEVED"
                End If
                'RSSdescription
                rssDetail = currentRssItem.SelectSingleNode("description")
                If Not IsNothing(rssDetail) Then
                    RSSdescription = rssDetail.InnerText
                Else
                    RSSdescription = "DESCRIPTION FAILED TO BE RETRIEVED"
                End If
                
                RSSFeed.Text = RSSFeed.Text & " " & RSStitle + " - " + RSSdescription
                ItemCount += 1
                Next
    Note that I move the Dim statement for the System.Xml.XmlNode outside the loop and added a counter so we can see the actual number of items you are processing. Please share the number of items processed when this code runs "slow" and give us an idea what "slow" means in seconds.
    • Marked As Answer by WithingtonUK Friday, September 14, 2012 9:49 AM
    •