工作中点滴记录

永远保持学徒心态

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

Rss是站点与站点之间共享内容的一种简易方式,也称为聚合内容,通常被用于新闻网站,网站提供rss输出,有利于用户发现网站的更新,网站用户可以再客户端借助于类似于资讯阅读器等支持rss的新闻聚合软件。

Rss是基于xml的一种形式,并且所有rss都要遵循W3C站点发布的XML 1.0规范,可以参见以下Rss2.0规范。

Rss 2.0规范

后台代码编写:

 

后台代码
 1  string xmlpath = @"rss.xml";
 2     protected void Page_Load(object sender, EventArgs e)
 3     {
 4         if (!IsPostBack)
 5         {
 6             string strPath = Server.MapPath(".") + xmlpath;
 7 
 8             XmlTextWriter writer = new XmlTextWriter(strPath, Encoding.UTF8);
 9             writer.Formatting = Formatting.Indented;
10             writer.WriteStartDocument();
11             writer.WriteComment("读入RSS");
12             writer.WriteStartElement("rss");
13             writer.WriteAttributeString("version", "2.0");
14             writer.WriteStartElement("channel");
15             writer.WriteStartElement("title");
16             writer.WriteCData("新闻要闻-新浪新闻");
17             writer.WriteEndElement();
18             writer.WriteStartElement("image");
19             writer.WriteStartElement("title");
20             writer.WriteCData("新闻中心");
21             writer.WriteEndElement();
22             writer.WriteElementString("link", "http://news.sina.com.cn");
23             writer.WriteElementString("url", "http://www.sinaimg.cn/home/deco/2009/0330/logo_home_news.gif");
24             writer.WriteEndElement();
25             writer.WriteStartElement("description");
26             writer.WriteCData("新闻中心-新闻要闻");
27             writer.WriteEndElement();
28             writer.WriteElementString("link", "http://roll.news.sina.com.cn/s/");
29             writer.WriteStartElement("language");
30             writer.WriteString("zh-cn");
31             writer.WriteEndElement();
32             writer.WriteStartElement("Copyright");
33             writer.WriteCData("Copyright 1996 - 2012 SINA Inc. All Rights Reserved");
34             writer.WriteEndElement();
35             DataTable dt = ReadData();
36             for (int i = 0; i < dt.Rows.Count; i++)
37             {
38                 string link = dt.Rows[i]["link"].ToString();
39                 string title = dt.Rows[i]["title"].ToString();
40                 string summary = dt.Rows[i]["summary"].ToString();
41                 string author = dt.Rows[i]["author"].ToString();
42                 string content = dt.Rows[i]["content"].ToString();
43                 string published = dt.Rows[i]["published"].ToString();
44                 writer.WriteStartElement("item");
45                 writer.WriteStartElement("title");
46                 writer.WriteString(title);
47                 writer.WriteEndElement();
48                 writer.WriteElementString("link", link);
49                 writer.WriteElementString("author", author);
50                 writer.WriteElementString("summary", summary);
51                 writer.WriteStartElement("description");
52                 writer.WriteString(content);
53                 writer.WriteEndElement();
54                 writer.WriteElementString("published", published);
55                 writer.WriteEndElement();
56             }
57             writer.WriteEndElement();
58             writer.WriteEndElement();
59             writer.WriteEndDocument();
60             writer.Flush();
61             writer.Close();
62 
63             XmlDocument document = new XmlDocument();
64             document.Load(strPath);
65             Response.ContentType = "text/xml";
66             document.Save(Response.Output);
67         }
68     }
69     public DataTable ReadData()
70     {
71         string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;
72         using (SqlConnection con = new SqlConnection(connectionString))
73         {
74             SqlCommand cmd = con.CreateCommand();
75             cmd.CommandText = "SELECT top 10 * FROM RSS WHERE summary='财经要闻汇总' order by [published] desc";
76             SqlDataAdapter da = new SqlDataAdapter(cmd);
77             DataSet ds = new DataSet();
78             da.Fill(ds);
79             if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)
80             {
81                 return new DataTable();
82             }
83             return ds.Tables[0];
84         }
85     }

 

由于IE7已经支持Rss信息的自动解析,我们能够看到下面的网页效果,否者看到只是xml代码文件。
样式截图:

 Rss内容读取

posted on 2012-08-04 18:39  梦里故乡  阅读(745)  评论(0编辑  收藏  举报