今天写了一个简单的新浪新闻RSS操作类库

今天,有位群友问我如何获新浪新闻列表相关问题,我想,用正则表达式网页中取显然既复杂又不一定准确,现在许多大型网站都有RSS集合,所以我就跟他说用RSS应该好办一些。

 

一年前我写过一个RSS阅读器,不过,打新浪相关的XML文件看了一下,发现RSS2.0 和一年前的不大一样了,但具体怎么处理,几句话也很难讲得清楚,所以,我干脆写了一个类库给他,直接调用。

 

类库不是很复杂,主要两个功能:

一、通过新浪的根频道XML在把所有频道的信息读出来,使用递归连同子节点也读取出来。

二、指定频道URL的XML文件来获取新闻信息。

 

首先,我们写两个类,一个用于保存新闻个息,另一个用于保存频道信息。

  1. /// <summary>  
  2. /// 新闻记录实体  
  3. /// </summary>  
  4. [Serializable]  
  5. public  class NewsItem  
  6. {  
  7.     /// <summary>  
  8.     /// 新闻标题  
  9.     /// </summary>  
  10.     public string Title { get; set; }  
  11.   
  12.     /// <summary>  
  13.     /// 新闻链接  
  14.     /// </summary>  
  15.     public string Link { get; set; }  
  16.   
  17.     /// <summary>  
  18.     /// 作者  
  19.     /// </summary>  
  20.     public string Author { get; set; }  
  21.   
  22.     /// <summary>  
  23.     /// 分类  
  24.     /// </summary>  
  25.     public string Category { get; set; }  
  26.   
  27.     /// <summary>  
  28.     /// 发布时间  
  29.     /// </summary>  
  30.     public DateTime PubDate { get; set; }  
  31.   
  32.     /// <summary>  
  33.     /// 描述  
  34.     /// </summary>  
  35.     public string Description { get; set; }  
  36.   
  37.     /// <summary>  
  38.     /// 其它说明  
  39.     /// </summary>  
  40.     public string Comments { get; set; }  
  41. }  


 

  1. /// <summary>  
  2. /// 新闻频道列表   
  3. /// </summary>  
  4. [Serializable]  
  5. public  class OutLine  
  6. {  
  7.     /// <summary>  
  8.     /// 频道标题  
  9.     /// </summary>  
  10.     public string Title { get; set; }  
  11.   
  12.     /// <summary>  
  13.     /// 频道文本  
  14.     /// </summary>  
  15.     public string Text { get; set; }  
  16.   
  17.     /// <summary>  
  18.     /// 频道类型-RSS  
  19.     /// </summary>  
  20.     public string Type { get; set; }  
  21.   
  22.     /// <summary>  
  23.     /// XML地址  
  24.     /// </summary>  
  25.     public string xmlUrl { get; set; }  
  26.   
  27.     /// <summary>  
  28.     /// HTML地址  
  29.     /// </summary>  
  30.     public string htmlUrl { get; set; }  
  31.   
  32.     private List<OutLine> _olChildren = new List<OutLine>();  
  33.   
  34.     /// <summary>  
  35.     /// 子频道  
  36.     /// </summary>  
  37.     public List<OutLine> ChildrenOutline  
  38.     {  
  39.         get { return _olChildren; }  
  40.     }  
  41. }  


 

好,接下来对应的两类,分别获取频道列表和新闻列表。

  1. /// <summary>  
  2. /// 新闻项管理类  
  3. /// </summary>  
  4. public class NewsManager  
  5. {  
  6.     /// <summary>  
  7.     /// 根据输入的XML地址获取新闻列表。  
  8.     /// </summary>  
  9.     /// <param name="xmlUrl">新闻频道的XML地址</param>  
  10.     /// <returns>NewsItem的结果集合</returns>  
  11.     public List<NewsItem> GetNewsItemList(string xmlUrl)  
  12.     {  
  13.         List<NewsItem> _myNews = new List<NewsItem>();  
  14.         XElement myRoot = XElement.Load(xmlUrl);  
  15.         var theItems =  
  16.             from xe in myRoot.Element("channel").Elements("item")  
  17.             select xe;  
  18.         foreach (XElement e in theItems)  
  19.         {  
  20.             _myNews.Add(new NewsItem()  
  21.             {  
  22.                 Title = (string)e.Element("title"),  
  23.                 Link = (string)e.Element("link"),  
  24.                 Author = (string)e.Element("author"),  
  25.                 Category = (string)e.Element("category"),  
  26.                 PubDate = (DateTime)e.Element("pubDate"),  
  27.                 Comments = (string)e.Element("comments"),  
  28.                 Description = (string)e.Element("description")  
  29.             });  
  30.         }  
  31.         return _myNews;  
  32.     }  
  33. }  


 

  1. /// <summary>  
  2. /// 自动获取频道列表类  
  3. /// </summary>  
  4. public class OutlineManager  
  5. {  
  6.     /// <summary>  
  7.     /// 获取频道列表,包含子节点  
  8.     /// </summary>  
  9.     /// <param name="xmlUrl">根频道地址</param>  
  10.     /// <returns></returns>  
  11.     public List<OutLine> GetCannels(string xmlUrl)  
  12.     {  
  13.         List<OutLine> _list = new List<OutLine>();  
  14.         XElement root = XElement.Load(xmlUrl);  
  15.         var firstOutline = root.Element("body").Elements("outline");  
  16.         foreach (XElement xitem in firstOutline)  
  17.         {  
  18.             OutLine myRootOutline = new OutLine  
  19.             {  
  20.                 Title = (string)xitem.Attribute("title") ?? "",  
  21.                 Text = (string)xitem.Attribute("text") ?? "",  
  22.                 Type = (string)xitem.Attribute("type") ?? "",  
  23.                 xmlUrl = (string)xitem.Attribute("xmlUrl") ?? "",  
  24.                 htmlUrl = (string)xitem.Attribute("htmlUrl") ?? ""  
  25.             };  
  26.             AddChildElements(xitem, myRootOutline);  
  27.             _list.Add(myRootOutline);  
  28.         }  
  29.         return _list;  
  30.     }  
  31.   
  32.   
  33.     private void AddChildElements(XElement xNode, OutLine ol)  
  34.     {  
  35.         if (xNode == null) return;  
  36.   
  37.         var xc = xNode.Elements("outline");  
  38.         // 递归,添加子节点  
  39.         foreach (XElement xe in xc)  
  40.         {  
  41.             OutLine outline = new OutLine()  
  42.             {  
  43.                 Title = xe.Attribute("title").Value,  
  44.                 Text = xe.Attribute("text").Value,  
  45.                 Type = xe.Attribute("type").Value,  
  46.                 xmlUrl = xe.Attribute("xmlUrl").Value,  
  47.                 htmlUrl = xe.Attribute("htmlUrl").Value  
  48.             };  
  49.   
  50.             ol.ChildrenOutline.Add(outline);  
  51.   
  52.             AddChildElements(xe, outline);  
  53.         }  
  54.     }  
  55. }  


 

OK,简单的类库写好了,程序集名称为SinaRssAPIs_CS,然后,我们建一个程序来测试一下。

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using SinaRssAPIs_CS;  
  10.   
  11. namespace NewsApiTest  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.             this.WindowState = FormWindowState.Maximized;  
  19.             this.Text = "新浪RSS类库示例程序";  
  20.             this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);  
  21.             this.dataGridView1.AutoGenerateColumns = false; //不自动创建列  
  22.             //添加列  
  23.             DataGridViewTextBoxColumn colTitle = new DataGridViewTextBoxColumn();  
  24.             colTitle.HeaderText = "新闻标题";  
  25.             colTitle.DataPropertyName = "Title";  
  26.             this.dataGridView1.Columns.Add(colTitle);  
  27.             DataGridViewTextBoxColumn colDesc = new DataGridViewTextBoxColumn();  
  28.             colDesc.HeaderText = "描述";  
  29.             colDesc.DataPropertyName = "Description";  
  30.             colDesc.Width = 280;  
  31.             this.dataGridView1.Columns.Add(colDesc);  
  32.             DataGridViewTextBoxColumn colDate = new DataGridViewTextBoxColumn();  
  33.             colDate.DefaultCellStyle.Format = "yyyy-MM-dd";  
  34.             colDate.HeaderText = "发布日期";  
  35.             colDate.DataPropertyName = "PubDate";  
  36.             this.dataGridView1.Columns.Add(colDate);  
  37.             DataGridViewTextBoxColumn colAuthor = new DataGridViewTextBoxColumn();  
  38.             colAuthor.HeaderText = "发布者";  
  39.             colAuthor.DataPropertyName = "Author";  
  40.             this.dataGridView1.Columns.Add(colAuthor);  
  41.             DataGridViewTextBoxColumn colLink = new DataGridViewTextBoxColumn();  
  42.             colLink.DataPropertyName = "Link";  
  43.             colLink.Name = "link";  
  44.             colLink.Visible = false;  
  45.             this.dataGridView1.Columns.Add(colLink);  
  46.   
  47.             this.dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);  
  48.         }  
  49.   
  50.         void dataGridView1_SelectionChanged(object sender, EventArgs e)  
  51.         {  
  52.             if (this.dataGridView1.CurrentRow == null) return;  
  53.             string link = this.dataGridView1.CurrentRow.Cells["link"].Value.ToString();  
  54.             this.webBrowser1.Navigate(link);  
  55.         }  
  56.   
  57.         void treeView1_AfterSelect(object sender, TreeViewEventArgs e)  
  58.         {  
  59.             if (e.Node.Tag == null) return;  
  60.   
  61.             string xml = e.Node.Tag.ToString();  
  62.             List<NewsItem> items = null;  
  63.             NewsManager mg = new NewsManager();  
  64.             items = mg.GetNewsItemList(xml);  
  65.   
  66.             this.dataGridView1.DataSource = items;  
  67.   
  68.         }  
  69.   
  70.   
  71.   
  72.         private void Form1_Load(object sender, EventArgs e)  
  73.         {  
  74.             OutlineManager omg = new OutlineManager();  
  75.             List<OutLine> cnList = omg.GetCannels(@"http://rss.sina.com.cn/sina_all_opml.xml");  
  76.             this.treeView1.BeginUpdate();  
  77.             this.treeView1.Nodes.Clear();  
  78.             //根节点  
  79.             foreach (OutLine  root in cnList)  
  80.             {  
  81.                 TreeNode tnRoot = new TreeNode();  
  82.                 tnRoot.Text = root.Title.Split('-')[0];  
  83.                 AddNodes(root, tnRoot);  
  84.                 this.treeView1.Nodes.Add(tnRoot);  
  85.             }  
  86.             this.treeView1.EndUpdate();  
  87.         }  
  88.   
  89.         private void AddNodes(OutLine ol, TreeNode nd)  
  90.         {  
  91.             foreach (OutLine oits in ol.ChildrenOutline)  
  92.             {  
  93.                 TreeNode tn = new TreeNode();  
  94.                 tn.Text = oits.Title;  
  95.                 tn.Tag = oits.xmlUrl;  
  96.                 AddNodes(oits, tn);  
  97.                 nd.Nodes.Add(tn);  
  98.             }  
  99.         }  
  100.     }  
  101. }  


大致的运行效果如下:

 

现在,我说一下技术要点,不多,就一个,对,就是LinQ To XML。

posted @ 2017-05-09 21:29  谢维开  阅读(201)  评论(0编辑  收藏  举报
友情链接:回力鞋官网 | 中老年高档女装 | 新航道 | 英语课堂游戏 | 托福和雅思