---
title: "rss 订阅"
author: "njmnjx"
date: "2010-11-11T20:50:47Z"
tags: []
categories: ["编程语言"]
slug: "rss"
draft: false
lastmod: "2010-11-11T20:50:47Z"
id: 809195
url: "https://njm.iteye.com/blog/809195"
---

```C#

//aspx页面内容
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

//aspx.cs内容
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;

public partial class _Default : System.Web.UI.Page 
{
    //页面直接加载
    protected void Page_Load(object sender, EventArgs e)
    {
        load();
    }

    //单击加载
    protected void imgBtn_Click(object sender, ImageClickEventArgs e)
    {
        load();
    }

    private void load()
    {
        //创建xml
        bool b = LogWrite(Server.MapPath("~/web/RssMessage.xml"));

        //转到xml
        //Response.Redirect("web/RssMessage-2010-11-11.xml");

        DataSet ds = new DataSet();


        string data = Server.MapPath("~/web/RssMessage.xml").ToString();
        if (b)
        {
            ds.ReadXml(data);
            string a = ds.GetXml();

            Response.Clear();
            Response.ContentType = "Text/XML";
            Response.Expires = 0;

            Response.Cache.SetNoStore();
            Response.Write(a);
            Response.End();
        }
    }




    private bool LogWrite(string filepath)
    {
        try
        {
            XmlDocument xdoc = new XmlDocument();
            //if (!File.Exists(filepath))
            //{
            xdoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><rss version=\"2.0\">" +
                         "</rss>");
            //}
            //else
            //{

            //    xdoc.Load(filepath);

            //}

            XmlNode root = xdoc.SelectSingleNode("rss");

            XmlElement xe_cha = xdoc.CreateElement("channel");

            //header
            XmlElement xe_Title = xdoc.CreateElement("title");
            xe_Title.InnerText = "此rss为会议列表";

            XmlElement xe_link = xdoc.CreateElement("link");
            xe_link.InnerText = "http://www.baidu.com";

            XmlElement xe_description = xdoc.CreateElement("description");
            xe_description.InnerText = "列表描述";

            xe_cha.AppendChild(xe_Title);
            xe_cha.AppendChild(xe_link);
            xe_cha.AppendChild(xe_description);

            //item
            for (int i = 0; i < 10; i++)
            {
                XmlElement xe_item = xdoc.CreateElement("item");
                xe_cha.AppendChild(xe_item);

                XmlElement item_Title = xdoc.CreateElement("title");
                item_Title.InnerText = "会议" + i;

                XmlElement item_link = xdoc.CreateElement("link");
                item_link.InnerText = "http://www.baidu.com?id=" + i;

                XmlElement item_description = xdoc.CreateElement("description");
                item_description.InnerText = "会议描述" + i;

                xe_item.AppendChild(item_Title);
                xe_item.AppendChild(item_link);
                xe_item.AppendChild(item_description);

            }


            root.AppendChild(xe_cha);

            // 保存存储信息的XML文件

            xdoc.Save(filepath);

            xdoc = null;
        }
        catch
        { }
        return true;
    }
}

//根目录创建web文件夹用于存放xml文件

//根目录创建images文件夹存放rss按钮图片，此按钮实现跳转到此页面的功能，此按键在其它的页面中。

```

![](http://dl.iteye.com/upload/attachment/346801/20dfeeb2-504f-3fb9-9554-f8544dc3ab9c.gif)

<span style="color:green">FireFox中的运行效果截图如下：</span>![](http://dl.iteye.com/upload/attachment/346803/fb78820f-e7af-3566-a960-189eb210181c.png)
<span style="color:green">IE中的运行效果截图如下：</span>

![](http://dl.iteye.com/upload/attachment/346805/a82333f1-f2c2-3595-af80-82a9b9bc5aff.png)

<span style="color:red">原码</span>
<http://dl.iteye.com/topics/download/401276cf-2bda-30ff-8e02-95ccd2a4573d>