使用DotNet来制作简单RSS频道

上一篇 / 下一篇  2007-01-11 10:59:48 / 个人分类:IT技术

技术:ASP.Net/C#/XML
  平台: IIS/.Framework1.0/MS SQL SERVER/IE or FireFox
 
  使用RSS v2.0文档结构如下

  <?xml version="1.0" encoding="utf-8" ?>
  <rss version="2.0">
      <channel>
          <title/><link/><language/><docs/><descrīption/><copyright/><generator/><lastBuildDate/><ttl/>
          <image>
              <url/><title/><link/>
          </image>
          <item>
              <title/><link/>            
              <descrīption>                     
                  <![CDATA[]]>               
              </descrīption>
              <category/><author/><pubDate/>
          </item>
      </channel>
  </rss>

  格式必须按照XML规范来写
  RSS2.0参考文献:http://blogs.law.harvard.edu/tech/rss
 
  定义读取RSS信息的存储过程
  CREATE PROCEDURE [dbo].[up_ListTopic]        --list topic record 列出留言主题
  AS
  SET NOCOUNT ON
  SET XACT_ABORT ON
      BEGIN TRANSACTION
      SELECT
      A.f_Tid,                --index 0
      A.f_Tname AS [标题:],            --index 1
      A.f_Tcontent AS [主题内容:],        --index 2
      A.f_Tip AS [留言者IP:],            --index 3
      A.f_Tadddate AS [留言日期:],        --index 4
      B.f_Uname AS [留言用户:],        --index 5
      A.f_ReplyHit AS [回复数:]        --index 6
      FROM dbo.tbl_topic AS A
      INNER JOIN dbo.tbl_Users AS B
      ON A.f_Uid=B.f_Uid
      ORDER BY A.f_Tadddate DESC
      COMMIT TRAN
 
  GO这里只用了这几个字段f_Tid, f_Tname, f_Tcontent, f_Tadddate, f_Uname
 
  下面便是RSS生成的dotnet_rss.aspx
 <%@ Page Language="C#" ContentType="application/xml" Debug="true" ResponseEncoding="utf-8" %>
 <%@ Import Namespace="System.Data" %>
 <%@ Import Namespace="System.Data.SqlClient" %>
 <scrīpt language="C#" runat="server">
      void Page_Load(Object src, EventArgs e){
          SqlConnection myConnection = new SqlConnection();        
          try{
              myConnection.ConnectionString="server=127.0.0.1;database=BBSDB;uid=sa;pwd=''";
              myConnection.Open();
              
              SqlCommand myCommand = new SqlCommand();
              myCommand.Connection = myConnection;
              myCommand.CommandType = CommandType.StoredProcedure;
              myCommand.CommandText = "[dbo].[up_listTopic]";
              
              SqlDataReader myDataReader = myCommand.ExecuteReader();        
                      
              //-----------make head detail-----------------
              StringBuilder xmlhead = new StringBuilder();    
              xmlhead.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
              xmlhead.Append("<rss version=\"2.0\">\n");
              xmlhead.Append("\t<channel>\n");
              xmlhead.Append("\t\t<title>MyBBS RSS Channel</title>\n");
              xmlhead.Append("\t\t<link>http://192.168.0.44/thebbs</link>\n");
              xmlhead.Append("\t\t<language>zh-cn</language>\n");
              xmlhead.Append("\t\t<docs>MyBBS RSS Center</docs>\n");
              xmlhead.Append("\t\t<descrīption>Latest 20 threads of all forums</descrīption>\n");
              xmlhead.Append("\t\t<copyright>Copyright(C) MyBBS</copyright>\n");
              xmlhead.Append("\t\t<generator>none</generator>\n");
              xmlhead.Append("\t\t<lastBuildDate>" + DateTime.Now + "</lastBuildDate>\n");
              xmlhead.Append("\t\t<ttl>90</ttl>\n");
              xmlhead.Append("\t\t<image>\n");
              xmlhead.Append("\t\t\t<url>http://127.0.0.1/thebbs/smile.gif</url>\n");
              xmlhead.Append("\t\t\t<title>MyBBS</title>\n");
              xmlhead.Append("\t\t\t<link>http://127.0.0.1/thebbs</link>\n");
              xmlhead.Append("\t\t</image>\n");
              
              //-----------make item-----------------        
              StringBuilder xmlitem = new StringBuilder();
              string descrīption = "";
              int i, num;
              num = 1;
              while(myDataReader.Read()){
                  //-----------read 20 record-----------------
                  if (num > 20) break;
                  num++;
                  descrīption = myDataReader[2].ToString();
                  if (descrīption.Length < 300)
                      i = descrīption.Length;
                  else
                      i = 300;            
                  descrīption = descrīption.Substring(0, i);
                  xmlitem.Append("\t\t<item>\n");
                  xmlitem.Append("\t\t\t<title>" + myDataReader[1].ToString() + "</title>\n");
                  xmlitem.Append("\t\t\t<link>http://127.0.0.1/thebbs/topic.aspx?Tid=" + myDataReader[0].ToString() + "</link>\n");
                  xmlitem.Append("\t\t\t<descrīption><![CDATA[" + descrīption + "]]></descrīption>\n");
                  xmlitem.Append("\t\t\t<category>none</category>\n");
                  xmlitem.Append("\t\t\t<author>" + myDataReader[5].ToString() + "</author>\n");
                  xmlitem.Append("\t\t\t<pubDate>" + myDataReader[4].ToString() + "</pubDate>\n");
                  xmlitem.Append("\t\t</item>\n");
              }
              myConnection.Close();
      
              //-----------make footer-----------------
              StringBuilder xmlfooter = new StringBuilder();        
              xmlfooter.Append("\t</channel>\n</rss>");
              
              //-----------Output Data-----------------
              Response.Write(xmlhead);
              Response.Write(xmlitem);
              Response.Write(xmlfooter);
          }catch(Exception ex){
 
              //-----------can not find DB process-----------------
              Response.Write("<ErrorMsg>Connection Database Error...</ErrorMsg>");            
          }
      }
 </scrīpt>

配好路径后用浏览器Go IE6.0的话没有继承RSS阅读只显示XML的结构..... OK就这样写完老
  P.S. 编辑器会自动替换掉一些字符 如:<scrīpt>中的 i ==> ī
 


TAG: IT技术

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-08-13  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 1481
  • 日志数: 27
  • 图片数: 3
  • 书签数: 7
  • 建立时间: 2007-01-08
  • 更新时间: 2008-02-27

RSS订阅

Open Toolbar