• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net和RSS的结合

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  
这几天一直浸淫在RSS的快乐中,便试着用asp.net做了一个RSS FeedXML我也学了很长时间,没什么建树,只到自己看到了RSS规范,呵呵感觉用RSS阅览器看RSS就像用outlook express访问新闻组一样,下面是我用vb.net写的一个asp.net页面xml.aspx,包括生成rss.xml文件

<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="VB" runat="server">
public sub Page_Load(Src as Object, e as EventArgs)
Dim xmlDoc as String = Server.MapPath("rss.xml"
Dim writer as XmlTextWriter

Dim sqlString As String
Dim myConnection As SQLConnection
Dim myCommand As SQLCommand
Dim myDataReader As SqlDataReader
Dim myConn As String = System.Configuration.ConfigurationSettings.AppSettings("Conn"
Dim i_id As String
Dim i_title As String
Dim i_content As String
Dim i_day As String
sqlString = "select top 10 id,i_title,i_content,i_day from information order by id desc"
myConnection = New SQLConnection( myConn )
myCommand = New SQLCommand( sqlString, myConnection )
myConnection.Open()
myDataReader = myCommand.ExecuteReader()

try
writer = new XmlTextWriter(xmlDoc,Encoding.UTF8)
writer.Formatting = Formatting.Indented
writer.WriteStartDocument(true)
writer.WriteComment("注释"
writer.WriteStartElement("rss"
writer.WriteAttributeString("version","0.92"
writer.WriteStartElement("channel"
writer.WriteStartElement("title"
writer.WriteString("频道名称"
writer.WriteEndElement()
writer.WriteStartElement("link"
writer.WriteString("http://你的网址/"
writer.WriteEndElement()
writer.WriteStartElement("description"
writer.WriteString("最新的十条信息!"
writer.WriteEndElement()
writer.WriteStartElement("language"
writer.WriteString("zh-cn"
writer.WriteEndElement()
''''循环开始
While myDataReader.Read()
i_id = myDataReader.GetInt32(0).ToString()
i_title = myDataReader.GetString(1).Trim()
i_content = myDataReader.GetString(2).Trim()
i_day = myDataReader.GetDateTime(3).ToString()

writer.WriteStartElement("item"
writer.WriteStartElement("title"
writer.WriteString(i_title)
writer.WriteEndElement()
writer.WriteStartElement("description"
writer.WriteCData(left(i_content,100))
writer.WriteEndElement()
writer.WriteStartElement("pubDate"
writer.WriteString(i_day)
writer.WriteEndElement()
writer.WriteStartElement("link"
writer.WriteString("http://你的网址/jnszf.aspx?
doc.Save(Response.Output)
catch exp as Exception
Response.Write(exp.ToString())
finally
if (NOT writer is Nothing) then
writer.Close()
end if
end try

myDataReader.Close()
myConnection.Close()
end sub
</script>

相同的效果如rss.aspx页面
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">
Sub Page_Load(Sender as object,e as eventargs)
Dim myConnection As SqlConnection
Dim myAdapter As SqlDataAdapter
Dim sqlString As String
Dim myDataSet As DataSet
Dim myConn As String = System.Configuration.ConfigurationSettings.AppSettings("Conn"
myConnection = New SQLConnection( myConn )
sqlString = "select top 10 * from information order by id desc"
myAdapter = New SQLDataAdapter( sqlString, myConnection )
myDataSet = New DataSet
myAdapter.Fill( myDataSet, "information" )
Response.ContentType = "Text/XML"
myRepeater.DataSource = myDataSet
myRepeater.DataBind()
End Sub
</script>
<?xml version="1.0" encoding="gb2312" ?>
<rss version="0.92" >
<channel>
<title>频道名称</title>
<link>http://你的网址/</link>
<description>最新的十条记录</description>
<language>zh-cn</language>
<asp:Repeater %></link>
</item>
</ItemTemplate>
</asp:Repeater>
</channel>
</rss>

这两个页面是我一天的心得,本文章只作资料备份之用,希望对各位看官有所帮助对了,最后别忘记配置web.config,数据库采用的是SQLserver 2000
<configuration>
<appSettings>
<add key="Conn" value="server=localhost;uid=数据库用户名;pwd=数据库用户密码;database=数据库" />
</appSettings>
</configuration>
 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
用ASP.NET建立一个在线RSS新闻聚合器(收藏)发布时间:2022-07-10
下一篇:
asp.net异常处理和错误页配置发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap