Bu makalemde vb.net kullanarak xml ile bir rss okuma programının nasıl yapıldığını göstereceğim. Böylelikle günümüzde yaygın olarak kullanılan rss teknolojisini yakından tanıyıp bilgi edinmiş olacağız.
Formumuzun design bölümünde aşağıdaki resimde göründüğü gibi sadece textbox, button, listview ve browser kullanıyoruz. Toolbox üzerine sağ tıklayıp add/remove items sekmesini tıklayıp, com components tab sekmesinden Microsoft web tarayıcısını formumuza ekliyebiliriz. Ayrıca projemize add web reference diyerek aşağıdaki resimde textboxta yazan adresi ekliyoruz.
Öncelikle projemize 3 adet class ekliyoruz. Bunların isimlerini RssChannel, RssItemCollection ve RssItem olarak veriyoruz. Kodları aşağıdaki gibi yazıyoruz. Öncelikle RssChannel isimli classımızı oaşağıdaki gibi oluşturuyoruz;
Imports System.Xml
Imports System.Xml.XPath
Public Class RssChannel
Private mTitle As String
Private mDescription As String
Private mLink As String
Private mItems As New RssItemCollection
Public Property Items() As RssItemCollection
Get
Return mItems
End Get
Set(ByVal Value As RssItemCollection)
mItems = Value
End Set
End Property
Public Property Title() As String
Get
Return mTitle
End Get
Set(ByVal Value As String)
mTitle = Value
End Set
End Property
Public Property Description() As String
Get
Return mDescription
End Get
Set(ByVal Value As String)
mDescription = Value
End Set
End Property
Public Property Link() As String
Get
Return mLink
End Get
Set(ByVal Value As String)
mLink = Value
End Set
End Property
Public Shared Function GetRssChannel(ByVal Url As String) As RssChannel
Dim Rc As New RssChannel
Dim Xd As XPathDocument
Try
Xd = New XPathDocument(Url) 'adresteki xml belgesini okuyor, ve bir belge gıbı goruyor
Catch ex As Exception
MsgBox(ex.Message) 'okuma sırasında bir hata verırse
End Try
Dim Xp As XPathNavigator = Xd.CreateNavigator 'ayırac oluşturuyor
Dim Ni As XPathNodeIterator 'bir ayaırac oluşturuyor verılen sorguya gore belge uzerınde ayırma ıslemını gerçeklesştiriyor
'title
Ni = Xp.Select("/rss/channel/title")
Ni.MoveNext()
Rc.Title = Ni.Current.Value
Ni = Xp.Select("/rss/channel/description")
Ni.MoveNext()
Rc.Description = Ni.Current.Value
Ni = Xp.Select("/rss/channel/link")
Ni.MoveNext()
Rc.Link = Ni.Current.Value
Ni = Xp.Select("/rss/channel/item")
While Ni.MoveNext
Dim Ri As New RssItem 'yeni bir haber oluşturuyor
Dim Ni2 As XPathNodeIterator
Ni2 = Ni.Current.Select("link") 'item>Link
Ni2.MoveNext()
Ri.Link = Ni2.Current.Value
Ni2 = Ni.Current.Select("description") 'item>description
Ni2.MoveNext()
Ri.Description = Ni2.Current.Value
Ni2 = Ni.Current.Select("title") 'item>title
Ni2.MoveNext()
Ri.Title = Ni2.Current.Value
Rc.Items.Add(Ri) ' haberi ekliyor Channel'a
End While
Return Rc
End Function
End Class
Şimdi ise RssItem isimli classımızı aşağıdaki gibi oluşturuyoruz;
Public Class RssItem
Private mTitle As String
Private mLink As String
Private mDescription As String
Public Property Title() As String
Get
Return mTitle
End Get
Set(ByVal Value As String)
mTitle = Value
End Set
End Property
Public Property Link() As String
Get
Return mLink
End Get
Set(ByVal Value As String)
mLink = Value
End Set
End Property
Public Property Description() As String
Get
Return mDescription
End Get
Set(ByVal Value As String)
mDescription = Value
End Set
End Property
End Class
Class oluşturmak işlemimizi RssItemCollection isimli classımızı da aşağıdaki gibi oluşturarak tamamlıyoruz;
Public Class RssItemCollection
Inherits CollectionBase
Public Function Add(ByVal Rssitem As RssItem) As Integer
Return list.Add(Rssitem)
End Function
Default Public Property Items(ByVal index As Integer) As RssItem
Get
Return List(index)
End Get
Set(ByVal Value As RssItem)
list(index) = Value
End Set
End Property
End Class1
En son olarak Formumuzun code behind kısmına aşağıdaki kodlarımızı yazıyoruz. Böylelikle adres getir buttonu tıklama işlemini, listview üzerindeki tek ve çift tıklama işlemlerini programlıyoruz;
Public Class Form1
Inherits System.Windows.Forms.Form
Dim RC As RssChannel
Private Sub BtnAdresGetir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdresGetir.Click
RC = RssChannel.GetRssChannel(txtAdres.Text)
Me.Text = RC.Title
Me.ListView1.Items.Clear()
For Each ri As RssItem In RC.Items
Dim Li As New ListViewItem(ri.Title)
Li.Tag = ri
ListView1.Items.Add(Li)
Next
End Sub
Private Sub ShowDescription(ByVal Desc As String)
Dim Fs As New IO.FileStream("Temp.htm", IO.FileMode.Create)
Dim Sw As New IO.StreamWriter(Fs, System.Text.Encoding.GetEncoding(1254))
Sw.Write("
Sw.Write(Desc)
Sw.Write("")
Sw.Close() : Fs.Close()
Me.Browser.Navigate2(IO.Path.Combine(Application.StartupPath, "Temp.htm"))
End Sub
Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
Dim Ri As RssItem = ListView1.SelectedItems(0).Tag
Me.Text = Ri.Title & " - " & RC.Title
ShowDescription(Ri.Description)
End Sub
Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
Dim Ri As RssItem = ListView1.SelectedItems(0).Tag
Me.Text = Ri.Link & " - " & RC.Title
Me.Browser.Navigate(Ri.Link)
End Sub
End Class
