Search This Blog

Tuesday, March 4, 2014

Rss Feed Reader Vb.net Example

VB.NET > XML > Rss Feed Reader






Example


 Dim url As String = "http://rss.cnn.com/rss/cnn_latest.rss"
        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)
        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("link", Type.GetType("System.String"))

        While i < rssItems.Count
            Dim node As XmlNode = rssItems.Item(i).SelectSingleNode("title")
            Dim title As String
            Dim link As String
            If node IsNot Nothing Then
                title = node.InnerText
            Else
                title = ""
            End If

            node = rssItems.Item(i).SelectSingleNode("link")
            If node IsNot Nothing Then
                link = node.InnerText
            Else
                link = ""
            End If
            Dim dr As DataRow = dt.NewRow()
            dr("title") = title
            dr("link") = link

            dt.Rows.Add(dr)

            i += 1
        End While

        DataGridView1.DataSource = dt