今天写了一个简单的新浪新闻RSS操作类库
今天,有位群友问我如何获新浪新闻列表相关问题,我想,用正则表达式网页中取显然既复杂又不一定准确,现在许多大型网站都有RSS集合,所以我就跟他说用RSS应该好办一些。
一年前我写过一个RSS阅读器,不过,打新浪相关的XML文件看了一下,发现RSS2.0 和一年前的不大一样了,但具体怎么处理,几句话也很难讲得清楚,所以,我干脆写了一个类库给他,直接调用。
类库不是很复杂,主要两个功能:
一、通过新浪的根频道XML在把所有频道的信息读出来,使用递归连同子节点也读取出来。
二、指定频道URL的XML文件来获取新闻信息。
首先,我们写两个类,一个用于保存新闻个息,另一个用于保存频道信息。
- /// <summary>
- /// 新闻记录实体
- /// </summary>
- [Serializable]
- public class NewsItem
- {
- /// <summary>
- /// 新闻标题
- /// </summary>
- public string Title { get; set; }
- /// <summary>
- /// 新闻链接
- /// </summary>
- public string Link { get; set; }
- /// <summary>
- /// 作者
- /// </summary>
- public string Author { get; set; }
- /// <summary>
- /// 分类
- /// </summary>
- public string Category { get; set; }
- /// <summary>
- /// 发布时间
- /// </summary>
- public DateTime PubDate { get; set; }
- /// <summary>
- /// 描述
- /// </summary>
- public string Description { get; set; }
- /// <summary>
- /// 其它说明
- /// </summary>
- public string Comments { get; set; }
- }
- /// <summary>
- /// 新闻频道列表
- /// </summary>
- [Serializable]
- public class OutLine
- {
- /// <summary>
- /// 频道标题
- /// </summary>
- public string Title { get; set; }
- /// <summary>
- /// 频道文本
- /// </summary>
- public string Text { get; set; }
- /// <summary>
- /// 频道类型-RSS
- /// </summary>
- public string Type { get; set; }
- /// <summary>
- /// XML地址
- /// </summary>
- public string xmlUrl { get; set; }
- /// <summary>
- /// HTML地址
- /// </summary>
- public string htmlUrl { get; set; }
- private List<OutLine> _olChildren = new List<OutLine>();
- /// <summary>
- /// 子频道
- /// </summary>
- public List<OutLine> ChildrenOutline
- {
- get { return _olChildren; }
- }
- }
好,接下来对应的两类,分别获取频道列表和新闻列表。
- /// <summary>
- /// 新闻项管理类
- /// </summary>
- public class NewsManager
- {
- /// <summary>
- /// 根据输入的XML地址获取新闻列表。
- /// </summary>
- /// <param name="xmlUrl">新闻频道的XML地址</param>
- /// <returns>NewsItem的结果集合</returns>
- public List<NewsItem> GetNewsItemList(string xmlUrl)
- {
- List<NewsItem> _myNews = new List<NewsItem>();
- XElement myRoot = XElement.Load(xmlUrl);
- var theItems =
- from xe in myRoot.Element("channel").Elements("item")
- select xe;
- foreach (XElement e in theItems)
- {
- _myNews.Add(new NewsItem()
- {
- Title = (string)e.Element("title"),
- Link = (string)e.Element("link"),
- Author = (string)e.Element("author"),
- Category = (string)e.Element("category"),
- PubDate = (DateTime)e.Element("pubDate"),
- Comments = (string)e.Element("comments"),
- Description = (string)e.Element("description")
- });
- }
- return _myNews;
- }
- }
- /// <summary>
- /// 自动获取频道列表类
- /// </summary>
- public class OutlineManager
- {
- /// <summary>
- /// 获取频道列表,包含子节点
- /// </summary>
- /// <param name="xmlUrl">根频道地址</param>
- /// <returns></returns>
- public List<OutLine> GetCannels(string xmlUrl)
- {
- List<OutLine> _list = new List<OutLine>();
- XElement root = XElement.Load(xmlUrl);
- var firstOutline = root.Element("body").Elements("outline");
- foreach (XElement xitem in firstOutline)
- {
- OutLine myRootOutline = new OutLine
- {
- Title = (string)xitem.Attribute("title") ?? "",
- Text = (string)xitem.Attribute("text") ?? "",
- Type = (string)xitem.Attribute("type") ?? "",
- xmlUrl = (string)xitem.Attribute("xmlUrl") ?? "",
- htmlUrl = (string)xitem.Attribute("htmlUrl") ?? ""
- };
- AddChildElements(xitem, myRootOutline);
- _list.Add(myRootOutline);
- }
- return _list;
- }
- private void AddChildElements(XElement xNode, OutLine ol)
- {
- if (xNode == null) return;
- var xc = xNode.Elements("outline");
- // 递归,添加子节点
- foreach (XElement xe in xc)
- {
- OutLine outline = new OutLine()
- {
- Title = xe.Attribute("title").Value,
- Text = xe.Attribute("text").Value,
- Type = xe.Attribute("type").Value,
- xmlUrl = xe.Attribute("xmlUrl").Value,
- htmlUrl = xe.Attribute("htmlUrl").Value
- };
- ol.ChildrenOutline.Add(outline);
- AddChildElements(xe, outline);
- }
- }
- }
OK,简单的类库写好了,程序集名称为SinaRssAPIs_CS,然后,我们建一个程序来测试一下。
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using SinaRssAPIs_CS;
- namespace NewsApiTest
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- this.WindowState = FormWindowState.Maximized;
- this.Text = "新浪RSS类库示例程序";
- this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
- this.dataGridView1.AutoGenerateColumns = false; //不自动创建列
- //添加列
- DataGridViewTextBoxColumn colTitle = new DataGridViewTextBoxColumn();
- colTitle.HeaderText = "新闻标题";
- colTitle.DataPropertyName = "Title";
- this.dataGridView1.Columns.Add(colTitle);
- DataGridViewTextBoxColumn colDesc = new DataGridViewTextBoxColumn();
- colDesc.HeaderText = "描述";
- colDesc.DataPropertyName = "Description";
- colDesc.Width = 280;
- this.dataGridView1.Columns.Add(colDesc);
- DataGridViewTextBoxColumn colDate = new DataGridViewTextBoxColumn();
- colDate.DefaultCellStyle.Format = "yyyy-MM-dd";
- colDate.HeaderText = "发布日期";
- colDate.DataPropertyName = "PubDate";
- this.dataGridView1.Columns.Add(colDate);
- DataGridViewTextBoxColumn colAuthor = new DataGridViewTextBoxColumn();
- colAuthor.HeaderText = "发布者";
- colAuthor.DataPropertyName = "Author";
- this.dataGridView1.Columns.Add(colAuthor);
- DataGridViewTextBoxColumn colLink = new DataGridViewTextBoxColumn();
- colLink.DataPropertyName = "Link";
- colLink.Name = "link";
- colLink.Visible = false;
- this.dataGridView1.Columns.Add(colLink);
- this.dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
- }
- void dataGridView1_SelectionChanged(object sender, EventArgs e)
- {
- if (this.dataGridView1.CurrentRow == null) return;
- string link = this.dataGridView1.CurrentRow.Cells["link"].Value.ToString();
- this.webBrowser1.Navigate(link);
- }
- void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
- {
- if (e.Node.Tag == null) return;
- string xml = e.Node.Tag.ToString();
- List<NewsItem> items = null;
- NewsManager mg = new NewsManager();
- items = mg.GetNewsItemList(xml);
- this.dataGridView1.DataSource = items;
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- OutlineManager omg = new OutlineManager();
- List<OutLine> cnList = omg.GetCannels(@"http://rss.sina.com.cn/sina_all_opml.xml");
- this.treeView1.BeginUpdate();
- this.treeView1.Nodes.Clear();
- //根节点
- foreach (OutLine root in cnList)
- {
- TreeNode tnRoot = new TreeNode();
- tnRoot.Text = root.Title.Split('-')[0];
- AddNodes(root, tnRoot);
- this.treeView1.Nodes.Add(tnRoot);
- }
- this.treeView1.EndUpdate();
- }
- private void AddNodes(OutLine ol, TreeNode nd)
- {
- foreach (OutLine oits in ol.ChildrenOutline)
- {
- TreeNode tn = new TreeNode();
- tn.Text = oits.Title;
- tn.Tag = oits.xmlUrl;
- AddNodes(oits, tn);
- nd.Nodes.Add(tn);
- }
- }
- }
- }
大致的运行效果如下:

现在,我说一下技术要点,不多,就一个,对,就是LinQ To XML。
今天写了一个简单的新浪新闻RSS操作类库的更多相关文章
- 自己写的一个简单PHP采集器
自己写的一个简单PHP采集器 <?php //**************************************************************** $url = &q ...
- 只是一个用EF写的一个简单的分页方法而已
只是一个用EF写的一个简单的分页方法而已 慢慢的写吧.比如,第一步,先把所有数据查询出来吧. //第一步. public IQueryable<UserInfo> LoadPagesFor ...
- 写了一个简单的CGI Server
之前看过一些开源程序的源码,也略微知道些Apache的CGI处理程序架构,于是用了一周时间,用C写了一个简单的CGI Server,代码算上头文件,一共1200行左右,难度中等偏上,小伙伴可以仔细看看 ...
- 写了一个简单可用的IOC
根据<架构探险从零开始写javaweb框架>内容写的一个简单的 IOC 学习记录 只说明了主要的类,从上到下执行的流程,需要分清主次,无法每个类都说明,只是把整个主线流程说清楚,避免 ...
- 写了一个简单的 Mybatis
写了一个简单的 Mybatis,取名 SimpleMybatis . 具备增删改查的基本功能,后续还要添加剩下的基本数据类型和Java集合类型的处理. 脑图中有完整的源码和测试的地址 http://n ...
- 门户级UGC系统的技术进化路线——新浪新闻评论系统的架构演进和经验总结(转)
add by zhj:先收藏了 摘要:评论系统是所有门户网站的核心标准服务组件之一.本文作者曾负责新浪网评论系统多年,这套系统不仅服务于门户新闻业务,还包括调查.投票等产品,经历了从单机到多机再到集群 ...
- Python爬虫:新浪新闻详情页的数据抓取(函数版)
上一篇文章<Python爬虫:抓取新浪新闻数据>详细解说了如何抓取新浪新闻详情页的相关数据,但代码的构建不利于后续扩展,每次抓取新的详情页时都需要重新写一遍,因此,我们需要将其整理成函数, ...
- 采集新浪新闻php插件
今天没事,就分享一个采集新浪新闻PHP插件接口,可用于火车头采集,比较简单,大家可以研究! 新浪新闻实时动态列表为:https://news.sina.com.cn/roll/?qq-pf-to=pc ...
- Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现
UI系列教程第八课:Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现 今天蓝老师要讲的是关于新浪新闻侧滑界面的实现.先看看原图: 如图所示,这种侧滑效果以另一种方式替 ...
随机推荐
- 【Linux】Ubuntu 开机默认亮度改动方法
换了ubuntu 之后.发现开机屏幕都是"最大亮度",每次都要到设置中手动调节,非常麻烦.于是想到去改动这个设置.Google一通,别人可行的办法到我这就没用了.郁闷.最后是在st ...
- wox 快速搜索程序
windows启动栏的搜索,会经常找不到exe. 使用wox可以非常快速的找到启动程序 https://github.com/Wox-launcher/Wox/ 安装完成后,默认alt+space出现 ...
- 检测含有挖矿脚本的WiFi热点——果然是天下没有免费的午餐
见:http://www.freebuf.com/articles/web/161010.html 本质上是在开放wifi热点,自己搭建挖掘的网页,让接入的人访问该网页. 802.11无线协议本身特点 ...
- ledisDB底层实现——本质上就是用leveldb这样的底层存储,和ssdb一样,meta里存的是hash、list等的元数据
Hash hash可以算是一种两级kv,首先通过key找到一个hash对象,然后再通过field找到或者设置相应的值. 在ledisdb里面,我们需要将key跟field关联成一个key,用来存放或者 ...
- RPC通信框架——RCF介绍
现有的软件中用了大量的COM接口,导致无法跨平台,当然由于与Windows结合的太紧密,还有很多无法跨平台的地方.那么为了实现跨平台,支持Linux系统,以及后续的分布式,首要任务是去除COM接口. ...
- 函数中的this的四种绑定形式
目录 this的默认绑定 this的隐式绑定 隐式绑定下,作为对象属性的函数,对于对象来说是独立的 在一串对象属性链中,this绑定的是最内层的对象 this的显式绑定:(call和bind方法) n ...
- 原生JS---5
原生js学习笔记5——BOM操作 什么是BOM BOM:Browser Object Model 是浏览器对象模型,浏览器对象模型提供了独立与内容的.可以与浏览器窗口进行互动的对象结构,BOM由多个对 ...
- idea导入ssm项目启动tomcat报错404
用idea写ssm项目,基于之前一直在用spring boot 对于idea如何运行ssm花费了一番功夫 启动Tom act一直在报404 我搜了网上各种解决办法都不行,花费一天多的时间解决不了 就 ...
- netty百万连接跟踪记录
0. 启动客户端和服务端 # 测试环境: centos7 jdk8 2核16G# 服务端启动nohup java -Xmx8192m -Xms4096m -XX:+UseG1GC -XX:Parall ...
- GS运维常用工具及文档
规范部分 GS产品线性能问题处理流程:http://gsk.inspur.com/File/t-4244 XXX项目性能问题信息收集单-模板:http://gsk.inspur.com/File/ ...