Blogroll为站点提供新闻聚合功能,根据RssUrl自动读取RssFeeds.
鉴于rss标准虽然早已成型,但大部分站点都没有遵守rss标准规范,因此在编写blogroll功能时,我仅获取rss的必需属性: title, link, pubDate!
RssItem: Rss元素对象,主要包括标题,链接,作者,发布日期等信息。
Blogroll: 由RssUrl获取RssItem并生成Blogroll的操作类。
2. Blogroll
鉴于rss标准虽然早已成型,但大部分站点都没有遵守rss标准规范,因此在编写blogroll功能时,我仅获取rss的必需属性: title, link, pubDate!
类关系图:
RssItem: Rss元素对象,主要包括标题,链接,作者,发布日期等信息。
Blogroll: 由RssUrl获取RssItem并生成Blogroll的操作类。
代码:
1. RssItem
1 /// <summary>
2 /// Rss Item Instance
3 /// </summary>
4 public class RssItem
5 {
6 /// <summary>
7 /// Create a new <see cref="BlogRoll.API.RssItem"/> instance with no parameters
8 /// </summary>
9 public RssItem() { }
10 /// <summary>
11 /// Title of RssItem
12 /// </summary>
13 public string Title { get; set; }
14 /// <summary>
15 /// Author of RssItem
16 /// </summary>
17 public string Author { get; set; }
18 /// <summary>
19 /// Link of RssItem
20 /// </summary>
21 public string Link { get; set; }
22 /// <summary>
23 /// Publish date of RssItem
24 /// </summary>
25 public DateTime PubDate { get; set; }
26 }
2 /// Rss Item Instance
3 /// </summary>
4 public class RssItem
5 {
6 /// <summary>
7 /// Create a new <see cref="BlogRoll.API.RssItem"/> instance with no parameters
8 /// </summary>
9 public RssItem() { }
10 /// <summary>
11 /// Title of RssItem
12 /// </summary>
13 public string Title { get; set; }
14 /// <summary>
15 /// Author of RssItem
16 /// </summary>
17 public string Author { get; set; }
18 /// <summary>
19 /// Link of RssItem
20 /// </summary>
21 public string Link { get; set; }
22 /// <summary>
23 /// Publish date of RssItem
24 /// </summary>
25 public DateTime PubDate { get; set; }
26 }
2. Blogroll
1 /// <summary>
2 /// Bllogroll class
3 /// </summary>
4 public sealed class Blogroll
5 {
6 /// <summary>
7 /// Load rss items from the url of rss feed
8 /// </summary>
9 /// <param name="RssUrl">rss url</param>
10 /// <param name="num">numbers</param>
11 /// <returns></returns>
12 public static List<RssItem> LoadRssItems(string RssUrl, int num)
13 {
14 List<RssItem> items = new List<RssItem>();
15 WebClient wc = new WebClient();
16 wc.Encoding = Encoding.UTF8;
17 XElement xe = null;
18 using (Stream stream = wc.OpenRead(RssUrl))
19 {
20 XmlTextReader xmlReader = new XmlTextReader(stream);
21
22 xe = XElement.Load(xmlReader);
23 }
24
25 var query = (
26 from q in xe.Element("channel").Elements("item")
27 orderby DateTime.Parse(q.Element("pubDate").Value) descending
28 select new RssItem
29 {
30 Title = q.Element("title").Value,
31 Link = q.Element("link").Value,
32 PubDate = DateTime.Parse(q.Element("pubDate").Value)
33 }).Take(num);
34
35 return query.ToList();
36 }
37 }
2 /// Bllogroll class
3 /// </summary>
4 public sealed class Blogroll
5 {
6 /// <summary>
7 /// Load rss items from the url of rss feed
8 /// </summary>
9 /// <param name="RssUrl">rss url</param>
10 /// <param name="num">numbers</param>
11 /// <returns></returns>
12 public static List<RssItem> LoadRssItems(string RssUrl, int num)
13 {
14 List<RssItem> items = new List<RssItem>();
15 WebClient wc = new WebClient();
16 wc.Encoding = Encoding.UTF8;
17 XElement xe = null;
18 using (Stream stream = wc.OpenRead(RssUrl))
19 {
20 XmlTextReader xmlReader = new XmlTextReader(stream);
21
22 xe = XElement.Load(xmlReader);
23 }
24
25 var query = (
26 from q in xe.Element("channel").Elements("item")
27 orderby DateTime.Parse(q.Element("pubDate").Value) descending
28 select new RssItem
29 {
30 Title = q.Element("title").Value,
31 Link = q.Element("link").Value,
32 PubDate = DateTime.Parse(q.Element("pubDate").Value)
33 }).Take(num);
34
35 return query.ToList();
36 }
37 }
效果:
ok, 下面使用此方法聚合codeproject rss(asp.net)的前10条生成blogroll:
source code from csdn
/**
*
* Licensed
* Under an Attribution, Share Alike License
*
* Copyright 2008-2014 Tjatse [ thisnamemeansnothing[at]gmail.com ]
*
**/