RSS 订阅功能的实现

很多站点多会用到RSS订阅功能,今天写了个,留个笔记。

 我们可以用.aspx,.ashx文件构建xml文件,输出xml 可以通过 XmlTextWriter实例化来输出,也可以直接输出。

1.利用XmlTextWriter输出

  

代码
     public  String uid  =   " 461 " ;
    
public  String UserInfo  =  String.Empty;
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
if  (Request.QueryString[ " id " !=   null   &&  Request.QueryString[ " id " ].ToString()  !=   "" )
        {
            uid 
=  Request.QueryString[ " id " ].ToString();
        }
        Response.ContentType 
=   " application/rss+xml " ;
        Response.ContentEncoding 
=  Encoding.UTF8;
        XmlTextWriter rsswriter 
=   new  XmlTextWriter(Response.OutputStream, Encoding.UTF8);

        WriteRssOpening(rsswriter);
        WriteRssBody(rsswriter);
        WriteRssEnding(rsswriter);
        rsswriter.Flush();
        Response.End();
    }


    
///   <summary>
    
///   信息标题
    
///   </summary>
    
///   <param name="rsswriter"></param>
     private   void  WriteRssOpening(XmlTextWriter rsswriter)
    {
        UserBasicModelInfo info 
=  UserBasic.GetBasicList(uid);
        rsswriter.WriteStartElement(
" rss " );
        rsswriter.WriteAttributeString(
" version " " 2.0 " );
        rsswriter.WriteStartElement(
" channel " );
        rsswriter.WriteElementString(
" title " , info.TrueName + " 的官方博客 " );
        rsswriter.WriteElementString(
" link " , Application[ " Url " +   " /Rss.aspx?id= "   +  uid  +   "" );
        rsswriter.WriteElementString(
" description " , info.TrueName  +   " 的官方博客 " );
    }

    
///   <summary>
    
///   信息整体
    
///   </summary>
    
///   <param name="rsswriter"></param>
     private   void  WriteRssBody(XmlTextWriter rsswriter)
    {
        DataTable GetDate 
=  wdxt_Common.GetSomeData.GetDataNum( " VW_NEWLOG " " * " " LOGSEECOMPETENCE=1 and userinfoid= "   +  uid  +   "" " addtime desc  " 100 ).Tables[ 0 ];

        
foreach  (DataRow rssitem  in  GetDate.Rows)
        {
            rsswriter.WriteStartElement(
" item " );
            rsswriter.WriteElementString(
" title " , rssitem[ " logtitle " ].ToString());
            rsswriter.WriteElementString(
" author " , rssitem[ " TRUENAME " ].ToString());
            rsswriter.WriteElementString(
" link " "" + Application[ " Url " ] + " /Log/show_ "   +  rssitem[ " logid " ].ToString()  +   " _6 " );
            rsswriter.WriteElementString(
" description " , rssitem[ " logcontent " ].ToString());
            rsswriter.WriteElementString(
" pubDate " , rssitem[ " addtime " ].ToString());
            rsswriter.WriteEndElement();
        }
    }

    
private   void  WriteRssEnding(XmlTextWriter rsswriter)
    {
        rsswriter.WriteEndElement();
        rsswriter.WriteEndElement();
    }   


2. 直接输出XML

  

代码
protected  String uid  =  String.Empty;
    
protected   void  Page_Load( object  sender, EventArgs e)
    {
        Response.Clear();
        uid 
=  HttpContext.Current.Request.QueryString[ " id " ].ToString();
       
        Response.ContentType 
=   " text/xml " ;
        Response.Charset 
=   " GB2312 " ;
        Response.Write(GetRSS());
    }

    
#region
    
///   <summary>
    
///  取得聚合文章
    
///   </summary>
     public   string  GetRSS()
    {
        UserBasicModelInfo info 
=  UserBasic.GetBasicList(uid);

        DataSet ds 
=   wdxt_Common.GetSomeData.GetDataNum( " VW_NEWLOG " " * " " LOGSEECOMPETENCE=1 and userinfoid= "   +  uid  +   "" " addtime desc  " 100 );
        String strRSS 
=   " <?xml version=\ " 1.0 \ "  encoding=\ " gb2312\ "  ?><rss version=\ " 2.0 \ " > " ;
        strRSS 
+=   " <channel> " ;
        strRSS 
+=   " <title> "   +  info.TrueName  +   " 的博客</title> " ;
        strRSS 
+=   " <link> "   +  Application[ " Url " +   " /Myspace/Space/Rss1.aspx?id= "   +  uid  +   " </link> " ;
        strRSS 
+=   " <description></description> " ;
        
for  ( int  i  =   0 ; i  <  ds.Tables[ 0 ].Rows.Count; i ++ )
        {
            
string  Logid  =  ds.Tables[ 0 ].Rows[i][ " logid " ].ToString();
            
string  LogContent  =  ds.Tables[ 0 ].Rows[i][ " LogContent " ].ToString();
            
string  Title  =  ds.Tables[ 0 ].Rows[i][ " logtitle " ].ToString();
            String LogAddTime 
=  ds.Tables[ 0 ].Rows[i][ " Addtime " ].ToString();
            
string  truename  =  ds.Tables[ 0 ].Rows[i][ " TrueName " ].ToString();

            strRSS 
+=   " <item> " ;
            strRSS 
+=   " <title> "   +  Title  +   " </title> " ;
            strRSS 
+=   " <link> "   +  Application[ " Url " +   " /Blog/info/ "   +  Logid  +   " </link> " ;
            strRSS 
+=   " <pubDate> "   +  LogAddTime  +   " </pubDate> " ;
            strRSS 
+=   " <copyright> "   +  truename  +   " </copyright> " ;
            strRSS 
+=   " <description><![CDATA[ "   +  LogContent  +   " ]]></description> " ;
            strRSS 
+=   " </item> " ;
        }
        strRSS 
+=   " </channel> " ;
        strRSS 
+=   " </rss> " ;
        
return  strRSS;
    }
    
#endregion


 

 

参考:1.  微软的CSASPNETRssFeeds

        2.  我的源码

        3. 可能还要为站点添加到别的网站的RRS链接,可以看看 博客园的。

你可能感兴趣的:(rss)