盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

读取符合RSS2.0规范的XML文档

时间:2024/12/7作者:未知来源:盾怪网教程人气:

[摘要]想试着做一个简单的读取RSS新闻的页面 虽然将.net中关于XML的几个类看了个遍但还是不太懂这是我写的一个读取XML文档的函数直接在设计页面中调用就行 Public Function LoadRS...
想试着做一个简单的读取RSS新闻的页面

虽然将.net中关于XML的几个类看了个遍

但还是不太懂

这是我写的一个读取XML文档的函数

直接在设计页面中调用就行

 

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 = "未找到信息源,您可刷新重试或联系管理员!"
                Exit Function
            End If

            If CStr(showNewsCount) = "" Or showNewsCount > 30 Then
                showNewsCount = 10        '默认新闻显示数目
            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 源数据出错!"

        End Try

    End Function




关键词:读取符合RSS2.0规范的XML文档




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版