注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

我只是一个人

我爱一个人

 
 
 

日志

 
 

Rss源,添加与订阅  

2011-08-15 10:09:03|  分类: 网页制作--技巧| |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

新建一个网站,添加类文件:用于创建rss源文件

Rss.cs

内部代码:

/****************************************************

 * 出处:Www.TraceFact.Net

 * 文章:在Web站点中创建和使用Rss

 * 路径:http://www.tracefact.net/Asp-Net/Creating-and-Consuming-Rss-Feeds-on-Your-Website

 * 日期:2007-9-17

 * 说明:用于生成 Rss 源的类,使用方法参见文章(跟随上面链接)

 * 作者:Bipin Joshi

 * 修改:张子阳(Jimmy Zhang)

 * *************************************************/

 

using System;

using System.IO;

using System.Data;

using System.Xml;

using System.Collections.Generic;

using System.Text;

 

public class Rss {

       public Stream OutputStream;

       public string RssTitle;

       public string PublisherUrl;

       public string Description;

       public string Copyright;

       public string Generator;

       public DataSet ItemSource;

       public string ItemTitleField;

       public string ItemUrlField;

       public string ItemDescriptionField;

       public string ItemPublicationDateField;

       public string ItemAuthor;

       public string ItemCategory;

 

       public static void PublishRss(Rss r){

              XmlTextWriter writer = new XmlTextWriter(r.OutputStream, Encoding.UTF8);

              writer.WriteStartDocument();

              writer.WriteStartElement("rss");

              writer.WriteAttributeString("version", "2.0");

              writer.WriteStartElement("channel");

              writer.WriteElementString("title", r.RssTitle);

              writer.WriteElementString("link", r.PublisherUrl);

              writer.WriteElementString("description", r.Description);

              writer.WriteElementString("copyright", r.Copyright);

              writer.WriteElementString("generator", r.Generator);

 

              foreach (DataRow row in r.ItemSource.Tables[0].Rows) {

            writer.WriteStartElement("item");

            writer.WriteElementString("id", row["Id"].ToString());

            writer.WriteElementString("author", row["Author"].ToString());

            writer.WriteElementString("category", row["Zhaiyao"].ToString());

            writer.WriteElementString("title", row["Title"].ToString());

            writer.WriteElementString("link", (row["Link"].ToString());

            writer.WriteElementString("description", row["Zhaiyao"].ToString());

            writer.WriteElementString("pubDate", GetRssDate(row["Times"]));

                     writer.WriteEndElement();

 

              }

 

              writer.WriteEndElement();

              writer.WriteEndElement();

              writer.Flush();

       }

 

       private static string GetRssDate(Object date) {

              DateTime rssDate = Convert.ToDateTime(date);

              string[] monthName = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

 

              StringBuilder sb = new StringBuilder();

              sb.Append(rssDate.Day);

              sb.Append(" ");

              sb.Append(monthName[Convert.ToInt32(rssDate.Month) - 1]);

              sb.Append(" ");

              sb.Append(rssDate.Year);

              sb.Append(" ");

              sb.Append(rssDate.ToLongTimeString());

 

              return sb.ToString();

       }

 

添加新建项:Rss_y.aspx页面只留下:用于读取Rss源:目前为止发现只能读取我上边方法生成的rss源

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Rss_y.aspx.cs" Inherits="_Default"  Debug="true"%>

 

代码也内容:

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

using System.Data.OleDb;

using System.Configuration;

 

public partial class _Default : System.Web.UI.Page

{

    Rss rss = new Rss();

    protected void Page_Load(object sender, EventArgs e)

    {

        DataSet ds = GetDataSet();

        if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)

        {

            Response.Write("数据源错误!");

            Response.End();

            return;

        }

        rss.OutputStream = Response.OutputStream;

        rss.RssTitle = "华森耐火材料";

        rss.PublisherUrl = Request.Url.Host;

        rss.Description = "DotNetBips.com - Applying .NET";

        rss.Copyright = "Copyright (C) DotNetBips.com.";

        rss.Generator = "DotNetBips.com RSS Generator";

        rss.ItemSource = ds;

        rss.ItemTitleField = "Title";

        rss.ItemDescriptionField = "Zhaiyao";

        rss.ItemPublicationDateField = "Times";

        rss.ItemUrlField = "Id";

        rss.ItemAuthor = "Author";

        Response.ContentEncoding = System.Text.Encoding.UTF8;

        Response.ContentType = "text/xml";

        Rss.PublishRss(rss);

        Response.End();

 

    }

    public DataSet GetDataSet()

    {

        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString() + HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["DbPath"]) + ";";

        OleDbConnection conn = new OleDbConnection(connectionString);

        OleDbDataAdapter da = new OleDbDataAdapter("select Id,Title, Author ,Zhaiyao, AddTime as Times,Bi  from Article  union select Id , Title,Remark as Author,Content as Zhaiyao,AddTime as Times,Bi from Products order by Id desc ", conn);

        DataSet ds = new DataSet();

        try

        {

            conn.Open();

            da.Fill(ds);

 

        }

        catch (Exception ex)

        {

            throw new Exception(ex.Message);

        }

        finally

        {

            conn.Close();

        }

        return ds;

    }

}

 

配置好你数据库,和数据库连接字符串

直接运行aspx页面即可:

Rss源,添加与订阅 - 坚持下来的人! - 我只是一个人

  

如果看到页面内容很乱的话,请新建浏览器输入地址即可看到

请用ie6以上版本查看

 

 

   

  评论这张
 
阅读(507)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018