zoukankan      html  css  js  c++  java
  • 让网站支持RSS订阅

    添加一个页面,例如Rss.aspx,里面的代码除了下面一行以外都删掉:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Rss.aspx.cs" Inherits="MyNamespace.Rss" %>

    Rss.aspx.cs代码实现:

    using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.IO;
    using System.Xml;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    
    namespace MyNamespace
    {
        public partial class Rss : System.Web.UI.Page
        {
            /// <summary>
            /// 输出XML格式数据
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Page_Load(object sender, EventArgs e)
            {
                if (IsPostBack) return;
    
                Response.Cache.SetNoStore();
                Response.ContentType = "application/xml";
    
                Response.Write(GetRSSOutput());
                Response.End();
            }
    
            /// <summary>
            /// 生成RSS内容
            /// </summary>
            /// <returns></returns>
            private string GetRSSOutput()
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    using (XmlTextWriter xmlTW = new XmlTextWriter(ms, Encoding.UTF8))
                    {
                        xmlTW.Formatting = Formatting.Indented;
                        xmlTW.WriteStartDocument();
                        xmlTW.WriteStartElement("rss");
                        xmlTW.WriteAttributeString("version", "2.0");
                        xmlTW.WriteStartElement("channel");
    
                        xmlTW.WriteElementString("title", "欢迎订阅 XXX网 - XXX频道 RSS");
                        xmlTW.WriteElementString("link", "http://www.xxxx.com/");
                        xmlTW.WriteElementString("description", "xxxx的描述");
    
                        // ---!!此处从你的Business Logic或数据处理层读取数据,输出给RSS---------------
                        // -- !!注意改成你自己的数据实体,List<你的实体>或DataSet,DataTable等-----
                        IList<MyEntityItem> list = BLL.EntityProvider.GetEntitiesList();
    
                        foreach (MyEntityItem var in list)
                        {
                            xmlTW.WriteStartElement("item");
                            
                            //----文章的标题-----
                            xmlTW.WriteElementString("title", var.Title);
    
                            //----文章的URL-----
                            xmlTW.WriteElementString("link", "http://www.xxx.com/yyy.aspx?id=" + var.ID.ToString());
    
                             //----文章发布时间----- 
                            xmlTW.WriteElementString("pubDate", string.Format("{0:R}", var.PostAt));
    
                             //----文章的作者-----
                            xmlTW.WriteElementString("author", var.AuthorName);
    
                             //----文章的描述-----
                            // --- 可以带<a href="xxx">yyy</a>, 如果多行,可以带" <br/>"
                            xmlTW.WriteElementString("description", var.Desc);
    
                            xmlTW.WriteEndElement();
                        }
    
                        xmlTW.WriteEndElement();
                        xmlTW.WriteEndElement();
                        xmlTW.WriteEndDocument();
                        xmlTW.Flush();
    
                        byte[] buffer = ms.ToArray();
                        return Encoding.UTF8.GetString(buffer);
                    }
                }
            }
        }
    }
    

    完成以后在IE里面输入该页面的URL可以看到RSS输出。

    (以上就是全部代码了,把MyNamespace改成你的namespace,把数据层对象改成你自己的就可以了!)

  • 相关阅读:
    top、ps -ef、ps aux的区别及内容详解
    img2pdf 报 img2pdf.AlphaChannelError: Refusing to work on images with alpha channel 的解决方案
    Scrapy命令行调用传入自定义参数
    查询Linux CPU架构
    LeetCode 216. 组合总和 III | Python
    LeetCode 40. 组合总和 II | Python
    LeetCode 39. 组合总和 | Python
    LeetCode 77. 组合 | Python
    LeetCode 347. 前 K 个高频元素 | Python
    LeetCode 107. 二叉树的层次遍历 II | Python
  • 原文地址:https://www.cnblogs.com/Mainz/p/1547938.html
Copyright © 2011-2022 走看看