Tips and tricks for .NET using ASP and VB code.

Load RSS feed into GridView

Option Compare Text
Partial Class Class1
Inherits System.Web.UI.Page
Dim WithEvents GridView1 As New GridView

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GridView1.ID = "GridView1"
GridView1.EmptyDataText = "No data"
Page.Form.Controls.Add(GridView1)
AddHandler GridView1.RowDataBound, AddressOf GridView1_RowDataBound
Dim GetWebData As New Net.WebClient
Dim XmlText As String = GetWebData.DownloadString("http://news.google.com/news?q=RSS&output=rss")
XmlText = FixCommonRssProblems(XmlText)
Dim RssDataView As Data.DataView = XmlText2DataView(XmlText, "item")
GridView1.DataSource = RssDataView
GridView1.DataBind()
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
For Each RowCell As TableCell In e.Row.Cells
RowCell.Text = HttpUtility.HtmlDecode(RowCell.Text)
If RowCell.Text Like "http:[/][/]*" Then
RowCell.ToolTip = RowCell.Text
RowCell.Text = "<a target='_blank' href='" & RowCell.Text & "'>Link</a>"
End If
Next
End Sub

Function XmlText2DataView(ByVal XmlText As String, _
Optional ByVal TableName As String = "") As Data.DataView
Dim XmlDataSet As New Data.DataSet
Dim CharacterEncoding As New UTF8Encoding
Dim ByteData As Byte() = CharacterEncoding.GetBytes(XmlText)
Dim MemoryStreamData As New IO.MemoryStream(ByteData)
Dim StreamData As IO.Stream = CType(MemoryStreamData, IO.Stream)
Try
XmlDataSet.ReadXml(StreamData)
Catch
End Try
If (TableName = "") And (XmlDataSet.Tables.Count > 0) Then
TableName = XmlDataSet.Tables(0).TableName
End If
Dim XmlDataView As New Data.DataView(XmlDataSet.Tables(TableName))
Return XmlDataView
End Function

Function FixCommonRssProblems(ByVal XmlText As String) As String
XmlText = XmlText.Replace("<entry", "<item")
XmlText = XmlText.Replace("</entry", "</item")
XmlText = XmlText.Replace("<link", "<_link")
XmlText = XmlText.Replace("</link", "</_link")
XmlText = XmlText.Replace("<title", "<_title")
XmlText = XmlText.Replace("</title", "</_title")
XmlText = XmlText.Replace("<description", "<_description")
XmlText = XmlText.Replace("</description", "</_description")
XmlText = XmlText.Replace("<language", "<_language")
XmlText = XmlText.Replace("</language", "</_language")
XmlText = XmlText.Replace("<copyright", "<_copyright")
XmlText = XmlText.Replace("</copyright", "</_copyright")
Return XmlText
End Function

End Class

No comments:

Post a Comment

Search This Blog