读取符合rss2.0规范的xml文档

作者:未知 来源:未知 添加时间:2006年7月3日 字体:

想试着做一个简单的读取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=/1fanwencom/viewnews.aspx@newstitle=" !amp; server.urlencode(newstitle) !amp; "!amp;newsurl=" !amp; server.urlencode(newsurl) !amp; _
                                    "!amp;newsdscrp=" !amp; server.urlencode(newsdescription) !amp; "!amp;newscat=" !amp; server.urlencode(newscategory) !amp; _
                                    "!amp;newsauthor=" !amp; server.urlencode(newsauthor) !amp; "!amp;newsdate=" !amp; server.urlencode(newspubdate) !amp; _
                                    " target=_blank>" !amp; newstitle !amp; "</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

ppdesk