欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

asp.net(c#) RSS功能实现代码

时间:2021-01-04 16:14:51|栏目:.NET代码|点击:
可能还有很多未完善,但终归可以使用了,以后再慢慢改进!!  
以下是我RSS界面的后台代码,给需要的朋友提供下我的经验:  
复制代码 代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.IO;
using System.Web.Configuration;
public partial class rss : System.Web.UI.Page
{
    string   HostUrl; 
    string   HttpHead;
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        HostUrl = context.Request.Url.ToString();
        HostUrl = HostUrl.Substring(0, HostUrl.IndexOf("/", 8));
        XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);
        WriteRSSPrologue(writer);
        WriteRSSHeadChennel(writer);
        string sql = "select top 10 title,id,time,content from blog_title order by time desc";
        SqlDataReader dr = dbconn.ExecuteReader(sql);
        while (dr.Read())
        {
            AddRSSItem(writer, (((DateTime)dr["time"]).ToUniversalTime()).ToString("r"), dr["title"].ToString(), HostUrl, dr["content"].ToString());
        }
        dr.Close();
        writer.Flush();
        writer.Close();
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;
        context.Response.ContentType = "text/xml";
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.End();
    }
    private XmlTextWriter WriteRSSPrologue(XmlTextWriter writer)
    {
        writer.WriteStartDocument();
        writer.WriteStartElement("rss");
        writer.WriteAttributeString("version", "2.0");
        writer.WriteAttributeString("xmlns:dc", "http://purl.org/dc/elements/1.1/");
        writer.WriteAttributeString("xmlns:trackbac", "http://madskills.com/public/xml/rss/module/trackback/");
        writer.WriteAttributeString("xmlns:wfw", "http://wellformedweb.org/CommentAPI/");
        writer.WriteAttributeString("xmlns:slash", "http://purl.org/rss/1.0/modules/slash/");
        return writer;
    }
    private XmlTextWriter WriteRSSHeadChennel(XmlTextWriter writer)
    {
            writer.WriteStartElement("channel");
            writer.WriteElementString("title", "编程博客(Nickeyj's Blog) - 最新日志");
            writer.WriteElementString("link", HostUrl + "/ ");
            writer.WriteElementString("description", "编程博客(Nickeyj's Blog)");
            writer.WriteElementString("copyright", "2008 www.52bcnet.com");
            writer.WriteElementString("generator", "编程博客(Nickeyj's Blog)   RSS   生成器   2.0 ");
        return writer;
    }
    private XmlTextWriter AddRSSItem(XmlTextWriter writer, string pubDate, string sItemTitle, string sItemLink, string sItemDescription)
    {
        writer.WriteStartElement("item");
        writer.WriteElementString("title", sItemTitle);
        writer.WriteElementString("link", sItemLink);
        writer.WriteElementString("description", sItemDescription);
        writer.WriteElementString("pubDate", pubDate);
        writer.WriteEndElement();
        return writer;
    }
    private XmlTextWriter AddRSSItem(XmlTextWriter writer, string sItemTitle, string sItemLink, string sItemDescription, bool bDescAsCDATA)
    {
        writer.WriteStartElement("item");
        writer.WriteElementString("title", sItemTitle);
        writer.WriteElementString("link", sItemLink);
        if (bDescAsCDATA == true)
        {
            writer.WriteStartElement("description");
            writer.WriteCData(sItemDescription);
            writer.WriteEndElement();
        }
        else
        {
            writer.WriteElementString("description", sItemDescription);
        }
        writer.WriteElementString("pubDate", DateTime.Now.ToString("r"));
        writer.WriteEndElement();
        return writer;
    }
    private XmlTextWriter WriteRSSClosing(XmlTextWriter writer)
    {
        writer.WriteEndElement();
        writer.WriteEndElement();
        writer.WriteEndDocument();
        return writer;
    }
}

上一篇:asp.net 不用GridView自带删除功能,删除一行数据

栏    目:.NET代码

下一篇:asp.net自定义控件代码学习笔记

本文标题:asp.net(c#) RSS功能实现代码

本文地址:http://www.codeinn.net/misctech/40526.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有