LINQ To XML And RSS

前陣子上課時展示過一個例子,以LINQ To XML來讀取RSS Feed,
當時我是使用離線的XML,最近有空把他改為直接讀取網路上的RSS.

前陣子上課時展示過一個例子,以LINQ To XML來讀取RSS Feed,

當時我是使用離線的XML,最近有空把他改為直接讀取網路上的RSS.

 

 

此例子使用VB.NET 2008寫成,如你所見,VB.NET於XML處理時,比C#來得簡潔.

 

01 Sub LoadRSS()
02         Dim request As System.Net.WebRequest = _
03            System.Net.WebRequest.Create("http://www.dotblogs.com.tw/code6421/category/1530.aspx/rss")
04         Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), System.Net.HttpWebResponse)
05         Dim dataStream As System.IO.Stream = response.GetResponseStream()
06         Dim reader As System.IO.StreamReader = New System.IO.StreamReader(dataStream)
07         Using (reader)
08             Dim xmlDoc = XDocument.Load(reader)
09             Dim result = From s1 In xmlDoc...<item> Select s1
10
11
12             For Each item In result
13                 Console.WriteLine(item.<title>.Value)
14             Next
15             Console.ReadLine()
16         End Using
17     End Sub