明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

讀取符合RSS2.0規(guī)范的XML文檔

[摘要]想試著做一個簡單的讀取RSS新聞的頁面 雖然將.net中關于XML的幾個類看了個遍但還是不太懂這是我寫的一個讀取XML文檔的函數(shù)直接在設計頁面中調(diào)用就行 Public Function LoadRSS(ByVal RssUrl As String, ByVal showNewsCount As I...
想試著做一個簡單的讀取RSS新聞的頁面

雖然將.net中關于XML的幾個類看了個遍

但還是不太懂

這是我寫的一個讀取XML文檔的函數(shù)

直接在設計頁面中調(diào)用就行

 

Public Function LoadRSS(ByVal RssUrl As String, ByVal showNewsCount As Integer) As String

        Try

            '讀取xml文檔
            Dim objXMLDoc As New System.Xml.XmlDocument()
            Dim strNodes As String = ""
            Dim objItems As System.Xml.XmlNodeList
            Dim objItems1 As System.Xml.XmlNodeList
            Dim objNode As System.Xml.XmlNode
            Dim objNode1 As System.Xml.XmlNode
            Dim i As Integer
            Dim newsTitle As String
            Dim newsUrl As String
            Dim newsDescription As String
            Dim newsPubDate As String
            Dim newsAuthor As String
            Dim newsCategory As String

            objXMLDoc.Load(RssUrl)

            objItems = objXMLDoc.GetElementsByTagName("item")

            If RssUrl = "" Then
                RSSNews = "未找到信息源,您可刷新重試或聯(lián)系管理員!"
                Exit Function
            End If

            If CStr(showNewsCount) = "" Or showNewsCount > 30 Then
                showNewsCount = 10        '默認新聞顯示數(shù)目
            End If

            If showNewsCount = 0 Then
                showNewsCount = objItems.Count
            End If

            If objXMLDoc.HasChildNodes = True Then
                i = 1
                For Each objNode In objItems

                    If objNode.HasChildNodes = True Then
                        objItems1 = objNode.ChildNodes
                        For Each objNode1 In objItems1

                            Select Case objNode1.Name
                                Case "title"
                                    newsTitle = objNode1.InnerText
                                Case "link"
                                    newsUrl = objNode1.InnerText
                                Case "description"
                                    newsDescription = objNode1.InnerText
                                    If Len(newsDescription) > 500 Then
                                        newsDescription = Left(newsDescription, 200)
                                    End If
                                    newsDescription = FilterHtml(newsDescription)
                               Case "category"
                                    newsCategory = objNode1.InnerText
                                Case "author"
                                    newsAuthor = objNode1.InnerText
                                Case "pubDate"
                                    newsPubDate = objNode1.InnerText
                            End Select


                        Next
                        strNodes += "<a href=viewnews.aspx?newstitle=" & Server.UrlEncode(newsTitle) & "&newsurl=" & Server.UrlEncode(newsUrl) & _
                                    "&newsdscrp=" & Server.UrlEncode(newsDescription) & "&newscat=" & Server.UrlEncode(newsCategory) & _
                                    "&newsauthor=" & Server.UrlEncode(newsAuthor) & "&newsdate=" & Server.UrlEncode(newsPubDate) & _
                                    " target=_blank>" & newsTitle & "</a><br>"
                    End If
                    i = i + 1
                    If i > showNewsCount Then Exit For
                Next
            End If

            LoadRSS = strNodes

        Catch objErr As Exception
            LoadRSS = "RSS Feed 源數(shù)據(jù)出錯!"

        End Try

    End Function