Ansel's Blog

It would probably render the application more complex

博客园 首页 新随笔 联系 订阅 管理

  
这几天一直浸淫在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?id="&i_id)
writer.WriteEndElement()
writer.WriteEndElement()
End While
''''循环结束
writer.WriteEndElement()
writer.WriteEndElement()
writer.Flush()
writer.Close()
Dim doc as XmlDocument = new XmlDocument()
doc.Load(xmlDoc)
Response.ContentType = "text/xml"
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 id="myRepeater" runat="server">
<ItemTemplate>
<item>
<title><%#DataBinder.Eval(Container.DataItem,"i_title"%></title>
<description><![CDATA[<%#DataBinder.Eval(Container.DataItem,"i_title"%>]]></description>
<pubDate><%#DataBinder.Eval(Container.DataItem,"i_day"%></pubDate>
<link>http://你的网址/jnszf.aspx?id=<%#DataBinder.Eval(Container.DataItem, "id" %></link>
</item>
</ItemTemplate>
</asp:Repeater>
</channel>
</rss>

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

posted on 2005-04-25 12:01  Ansel  阅读(967)  评论(1编辑  收藏  举报