标题:C#读取Rss并返回DataTable
只看楼主
saiyo55
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2010-8-5
结帖率:0
 问题点数:0 回复次数:0 
C#读取Rss并返回DataTable
最近需要用(c#)写一个Rss的接受。

接受的是自己论坛的Rss

而Rss是又在Item中加了一个子项。

所以写了一个通用的读取Rss的代码,以后即时再增加或者删除Items中的子项,此代码也是可以辨认的。

嗯……贴代码,记录一下吧。

 

 读取Rss返回DataTable
 1 /// <summary> 2         /// 获Rss并转换为DataTable返回 3         /// </summary> 4         /// <param name="filePath">Rss地址</param> 5         /// <returns></returns> 6          public static DataTable GetRss(string filePath) 7         { 8             DataTable dt = new DataTable(); 9             WebClient wc = new WebClient();10             Stream srContent = wc.OpenRead(filePath);11 12             StreamReader sr = new StreamReader(srContent);13             if (!sr.EndOfStream)14             {15                 XmlDocument xmlDoc = new XmlDocument();16                 xmlDoc.Load(sr);17                 XmlNodeList xnl = xmlDoc.SelectNodes("rss/channel/item");18                 if (xnl.Count > 0)19                 {20                     {//为dataTable添加列标识开始21                         XmlNode xnColumn = xnl[0];22                         XmlNodeList columnsNode = xnColumn.ChildNodes;23                         foreach (XmlNode xn in columnsNode)24                         {25                             DataColumn dc = new DataColumn(xn.Name);26                             dt.Columns.Add(dc);27                         }28                     }//为dataTable添加列标识结束29 30                     {//为DataTable添加行数据31                         foreach (XmlNode xnDate in xnl)32                         {33                             DataRow dr = dt.NewRow();34                             for (int i = 0; i < dt.Columns.Count; i++)35                             {36                                 XmlNode itemValue = xnDate.SelectSingleNode(dt.Columns[i].ColumnName);37                                 dr[i] = itemValue.InnerText;38                             }39                             dt.Rows.Add(dr);40                         }41                     }//为DataTable添加列数据42                 }43             }44             return dt;45         }
搜索更多相关主题的帖子: DataTable Rss 
2010-10-26 16:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-324058-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.019175 second(s), 7 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved