博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Rss内容读取
阅读量:6879 次
发布时间:2019-06-27

本文共 23298 字,大约阅读时间需要 77 分钟。

有时候会遇见要读取网站提供的rss,这时候需要用到XmlDocument类来实现。

以博客园为例,我的博客的rss地址是,把读取后的内容插入到数据库中,

代码如下:

table
1 CREATE TABLE [Rss](2     [id] [int] IDENTITY(1,1) primary key NOT NULL,3     [link] [nvarchar](300) NULL,4     [title] [nvarchar](200) NULL,5     [summary] [nvarchar](max) NULL,6     [author] [nvarchar](50) NULL,7     [content] [nvarchar](max) NULL,8     [published] [date] NULL,9     )

操作代码:

操作代码
1 public partial class _Default : System.Web.UI.Page  2 {  3     protected void Page_Load(object sender, EventArgs e)  4     {  5         if (!IsPostBack)  6         {  7             WriteDataTable();  8             BindGridView();  9         } 10     } 11     public DataTable CreateXmlTable() 12     { 13         DataTable dt = new DataTable(); 14         dt.Columns.Add("Link", typeof(string)); 15         dt.Columns.Add("title", typeof(string)); 16         dt.Columns.Add("summary", typeof(string)); 17         dt.Columns.Add("author", typeof(string)); 18         dt.Columns.Add("content", typeof(string)); 19         dt.Columns.Add("published", typeof(string)); 20         return dt; 21     } 22  23     public void WriteDataTable() 24     { 25         DataTable dt = CreateXmlTable(); 26         string strRss = "http://www.cnblogs.com/hfliyi/rss"; 27         XmlDocument docment = new XmlDocument(); 28         docment.Load(strRss); 29         XmlNodeList list = docment.GetElementsByTagName("entry"); 30         if (docment.HasChildNodes) 31         { 32             foreach (XmlNode var in list) 33             { 34                 DataRow dr = dt.NewRow(); 35                 if (var.HasChildNodes == true) 36                 { 37                     XmlNodeList objList = var.ChildNodes; 38                     foreach (XmlNode v in objList) 39                     { 40                         if (v.Name.ToLower() == "title") 41                         { 42                             dr["title"] = v.InnerText; 43                         } 44                         if (v.Name.ToLower() == "id") 45                         { 46                             dr["link"] = v.InnerText; 47                         } 48                         if (v.Name.ToLower() == "summary") 49                         { 50                             dr["summary"] = v.InnerText; 51                         } 52                         if (v.Name.ToLower() == "author") 53                         { 54                             dr["author"] = v.InnerText; 55                         } 56                         if (v.Name.ToLower() == "content") 57                         { 58                             dr["content"] = v.InnerText; 59                         } 60                         if (v.Name.ToLower() == "published") 61                         { 62                             dr["published"] = v.InnerText; 63                         } 64                     } 65                 } 66                 dt.Rows.Add(dr); 67             } 68         } 69         WriteToSQL(dt); 70     } 71  72     public void WriteToSQL(DataTable dt) 73     { 74  75         if (dt == null || dt.Rows.Count <= 0) 76         { 77             return; 78         } 79         for (int i = 0; i < dt.Rows.Count; i++) 80         { 81             RssModel model = new RssModel(); 82             model.Link = dt.Rows[i]["link"].ToString(); 83             model.Author = dt.Rows[i]["author"].ToString(); 84             model.Content = dt.Rows[i]["content"].ToString(); 85             model.Published = Convert.ToDateTime(dt.Rows[i]["published"].ToString()); 86             model.Summary = dt.Rows[i]["summary"].ToString(); 87             model.Title = dt.Rows[i]["title"].ToString(); 88             if (!IsExistsByLink(model.Link)) 89             { 90                 ExcuteNonQuery(model); 91             } 92         } 93     } 94     public void ExcuteNonQuery(RssModel model) 95     { 96         string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; 97         using (SqlConnection con = new SqlConnection(connectionString)) 98         { 99             con.Open();100             SqlCommand cmd = con.CreateCommand();101             cmd.CommandText = "INSERT INTO Rss([link],[title],[summary],[author],[content],[published]) VALUES(@link,@title,@summary,@author,@content,@published)";102             SqlParameter[] sp ={ 103             new SqlParameter("@link",SqlDbType.NVarChar,300),104                     new SqlParameter("@title",SqlDbType.NVarChar,200),105                 new SqlParameter("@summary",SqlDbType.NVarChar,4000),106                 new SqlParameter("@author",SqlDbType.NVarChar,50),107                 new SqlParameter("@content",SqlDbType.NVarChar,4000),108                 new SqlParameter("@published",SqlDbType.Date)109             };110             sp[0].Value = model.Link;111             sp[1].Value = model.Title;112             sp[2].Value = model.Summary;113             sp[3].Value = model.Author;114             sp[4].Value = model.Content;115             sp[5].Value = Convert.ToDateTime(model.Published);116             cmd.Parameters.AddRange(sp);117             cmd.ExecuteNonQuery();118         }119     }120     public void BindGridView()121     {122         string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;123         using (SqlConnection con = new SqlConnection(connectionString))124         {125             SqlCommand cmd = con.CreateCommand();126             cmd.CommandText = "SELECT * FROM RSS";127             SqlDataAdapter da = new SqlDataAdapter(cmd);128             DataSet ds = new DataSet();129             da.Fill(ds);130             this.gv_state.DataSource = ds.Tables[0];131             this.gv_state.DataBind();132         }133     }134     public bool IsExistsByLink(string link)135     {136         string ConnectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString.ToString();137         using (SqlConnection con = new SqlConnection(ConnectionString))138         {139             con.Open();140             string strSql = String.Format("SELECT COUNT(0) FROM RSS WHERE LINK='{0}'", link);141             SqlCommand cmd = con.CreateCommand();142             cmd.CommandText = strSql;143             object ob = cmd.ExecuteScalar();144             return Convert.ToInt32(ob) > 0;145         }146     }147 148 }149 public class RssModel150 {151     public RssModel()152     {153     }154     private string link;155     public string Link156     {157         get { return link; }158         set { link = value; }159     }160 161     private string title;162     public string Title163     {164         get { return title; }165         set { title = value; }166     }167 168     private string summary;169     public string Summary170     {171         get { return summary; }172         set { summary = value; }173     }174 175     private string author;176     public string Author177     {178         get { return author; }179         set { author = value; }180     }181 182     private string content;183     public string Content184     {185         get { return content; }186         set { content = value; }187     }188 189     private DateTime published;190     public DateTime Published191     {192         get { return published; }193         set { published = value; }194     }195 }

前台源代码:

前台原代码
1             
3
4
5
6
7 <%#Eval("title")%> 8 9
10
11
12
13
14 <%#((System.Data.DataRowView)Container.DataItem)["author"]%>15
16
17
18
19
20 <%#((System.Data.DataRowView)Container.DataItem)["published"]%>21
22
23
24
25
26 <%#Eval("summary").ToString().Length>20?Eval("summary").ToString().Substring(0,20):Eval("summary")%>27
28
29
30
31
32
33
34 35 36 文章标题37 38 39 作者40 41 42 发布日期43 44 45 摘要46 47 48 49 50
当前没有查询记录51 52 53
54

 例如读取新浪财经rss,代码如下:

View Code
1 public partial class Default2 : System.Web.UI.Page  2 {  3     protected void Page_Load(object sender, EventArgs e)  4     {  5         if (!IsPostBack)  6         {  7             ReadRss();  8             BindGridView();  9         } 10     } 11     public DataTable CreateXmlTable() 12     { 13         DataTable dt = new DataTable(); 14         dt.Columns.Add("Link", typeof(string)); 15         dt.Columns.Add("title", typeof(string)); 16         dt.Columns.Add("summary", typeof(string)); 17         dt.Columns.Add("author", typeof(string)); 18         dt.Columns.Add("content", typeof(string)); 19         dt.Columns.Add("published", typeof(string)); 20         return dt; 21     } 22     public void BindGridView() 23     { 24         string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; 25         using (SqlConnection con = new SqlConnection(connectionString)) 26         { 27             SqlCommand cmd = con.CreateCommand(); 28             cmd.CommandText = "SELECT * FROM RSS WHERE summary='财经要闻汇总'"; 29             SqlDataAdapter da = new SqlDataAdapter(cmd); 30             DataSet ds = new DataSet(); 31             da.Fill(ds); 32             this.gv_state.DataSource = ds.Tables[0]; 33             this.gv_state.DataBind(); 34         } 35     } 36     public void ReadRss() 37     { 38         DataTable dt = CreateXmlTable(); 39         string rss = @"http://rss.sina.com.cn/roll/finance/hot_roll.xml"; 40         XmlDocument document = new XmlDocument(); 41         document.Load(rss); 42         XmlNodeList list = document.GetElementsByTagName("item"); 43         if (document.HasChildNodes) 44         { 45             foreach (XmlNode var in list) 46             { 47                 DataRow dr = dt.NewRow(); 48                 if (var.HasChildNodes) 49                 { 50                     XmlNodeList v = var.ChildNodes; 51                     foreach (XmlNode vv in v) 52                     { 53                         switch (vv.Name) 54                         { 55                             case "title": 56                                 dr["title"] = vv.InnerText; 57                                 break; 58                             case "author": 59                                 dr["author"] = vv.InnerText; 60                                 break; 61                             case "link": 62                                 dr["Link"] = vv.InnerText; 63                                 break; 64                             case "category": 65                                 dr["summary"] = vv.InnerText; 66                                 break; 67                             case "description": 68                                 dr["content"] = vv.InnerText; 69                                 break; 70                             case "pubDate": 71                                 dr["published"] = vv.InnerText; 72                                 break; 73                             default: 74                                 break; 75                         } 76                     } 77                 } 78                 dt.Rows.Add(dr); 79             } 80             WriteToSQL(dt); 81         } 82     } 83     public void ExcuteNonQuery(RssMod model) 84     { 85         string connectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; 86         using (SqlConnection con = new SqlConnection(connectionString)) 87         { 88             con.Open(); 89             SqlCommand cmd = con.CreateCommand(); 90             cmd.CommandText = "INSERT INTO Rss([link],[title],[summary],[author],[content],[published]) VALUES(@link,@title,@summary,@author,@content,@published)"; 91             SqlParameter[] sp ={  92             new SqlParameter("@link",SqlDbType.NVarChar,300), 93                     new SqlParameter("@title",SqlDbType.NVarChar,200), 94                 new SqlParameter("@summary",SqlDbType.NVarChar,4000), 95                 new SqlParameter("@author",SqlDbType.NVarChar,50), 96                 new SqlParameter("@content",SqlDbType.NVarChar,4000), 97                 new SqlParameter("@published",SqlDbType.Date) 98             }; 99             sp[0].Value = model.Link;100             sp[1].Value = model.Title;101             sp[2].Value = model.Summary;102             sp[3].Value = model.Author;103             sp[4].Value = model.Content;104             sp[5].Value = Convert.ToDateTime(model.Published);105             cmd.Parameters.AddRange(sp);106             cmd.ExecuteNonQuery();107         }108     }109     public bool IsExistsByLink(string link)110     {111         string ConnectionString = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString.ToString();112         using (SqlConnection con = new SqlConnection(ConnectionString))113         {114             con.Open();115             string strSql = String.Format("SELECT COUNT(0) FROM RSS WHERE LINK='{0}'", link);116             SqlCommand cmd = con.CreateCommand();117             cmd.CommandText = strSql;118             object ob = cmd.ExecuteScalar();119             return Convert.ToInt32(ob) > 0;120         }121     }122     public void WriteToSQL(DataTable dt)123     {124 125         if (dt == null || dt.Rows.Count <= 0)126         {127             return;128         }129         for (int i = 0; i < dt.Rows.Count; i++)130         {131             RssMod model = new RssMod();132             model.Link = dt.Rows[i]["link"].ToString();133             model.Author = dt.Rows[i]["author"].ToString();134             model.Content = dt.Rows[i]["content"].ToString();135             model.Published = Convert.ToDateTime(dt.Rows[i]["published"].ToString());136             model.Summary = dt.Rows[i]["summary"].ToString();137             model.Title = dt.Rows[i]["title"].ToString();138             if (!IsExistsByLink(model.Link))139             {140                 ExcuteNonQuery(model);141             }142         }143     }144 }145 public class RssMod146 {147     public RssMod()148     {149     }150     private string link;151     public string Link152     {153         get { return link; }154         set { link = value; }155     }156 157     private string title;158     public string Title159     {160         get { return title; }161         set { title = value; }162     }163 164     private string summary;165     public string Summary166     {167         get { return summary; }168         set { summary = value; }169     }170 171     private string author;172     public string Author173     {174         get { return author; }175         set { author = value; }176     }177 178     private string content;179     public string Content180     {181         get { return content; }182         set { content = value; }183     }184 185     private DateTime published;186     public DateTime Published187     {188         get { return published; }189         set { published = value; }190     }191 }

 -------

读取博客园rss

Model
1 namespace NeoModel 2 { 3     public class RssReader 4     { 5         public RssReader() 6         { 7         } 8         private int _id; 9         public int ID10         {11             get { return _id; }12             set { _id = value; }13         }14 15         private string _title;16         public string Title17         {18             get { return _title; }19             set { _title = value; }20         }21 22         private DateTime? _pubdate;23         public DateTime? PubDate24         {25             get { return _pubdate; }26             set { _pubdate = value; }27         }28 29         private string _guid;30         public string Guid31         {32             get { return _guid; }33             set { _guid = value; }34         }35 36         private string _description;37         public string Description38         {39             get { return _description; }40             set { _description = value; }41         }42     }43 }
DAL
1 namespace NeoDAL 2 { 3     public class RssReaderDal 4     { 5         public RssReaderDal() 6         { 7         } 8         ///  9         /// 添加rss10         /// 11         /// 12         /// 
13 public bool AddRssReader(NeoModel.RssReader rss)14 {15 string strSql = "Insert into RssReader(title,pubdate,[guid],[description]) values(@title,@pubdate,@guid,@description)";16 SqlParameter[] sp = { 17 new SqlParameter("@title",rss.Title),18 new SqlParameter("@pubdate",rss.PubDate),19 new SqlParameter("@guid",rss.Guid),20 new SqlParameter("@description",rss.Description)21 };22 return DbHelperSQL.ExecuteSql(strSql, sp) > 0;23 24 }25 /// 26 /// 判断rss是否存在27 /// 28 /// 29 ///
30 public bool IsExistRssByGuid(string guid)31 {32 string strSql = "SELECT COUNT(0) FROM RssReader WHERE [guid]=@guid";33 SqlParameter[] sp = { 34 new SqlParameter("@guid",guid)35 };36 return Convert.ToInt32(DbHelperSQL.GetSingle(strSql, sp)) > 0;37 }38 /// 39 /// 40 /// 41 ///
42 public DataTable GetRss()43 {44 string strSql = "Select * from RssReader order by pubdate desc";45 DataSet ds = DbHelperSQL.Query(strSql);46 if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)47 {48 return new DataTable();49 }50 return ds.Tables[0];51 }52 53 }54 }
BLL
1 namespace NeoBLL 2 { 3     public class RssReader 4     { 5         NeoDAL.RssReaderDal dal = new NeoDAL.RssReaderDal(); 6         public RssReader() { } 7  8         ///  9         /// 10         /// 11         /// 12         /// 
13 public bool AddRssReader(NeoModel.RssReader rss)14 {15 return dal.AddRssReader(rss);16 }17 /// 18 /// 19 /// 20 /// 21 ///
22 public bool IsExistRssByGuid(string guid)23 {24 return dal.IsExistRssByGuid(guid);25 }26 public DataTable GetRss()27 {28 return dal.GetRss();29 }30 }31 }
页面源
1 
2
73
74
75
76
77
78
79

80 " target="_blank">81 <%#Eval("title") %>

82

83 <%#Eval("pubdate")%>84

85
86
87 <%#Eval("description")%>
88 89
90
93
94
95
96
后台
1 namespace NeoBackground.Baidu 2 { 3     public partial class RssReader : System.Web.UI.Page 4     { 5         NeoBLL.RssReader bll = new NeoBLL.RssReader(); 6         protected void Page_Load(object sender, EventArgs e) 7         { 8             if (!IsPostBack) 9             {10                 // WriterRss();11                 DataTable dt = bll.GetRss();12                 this.reprss.DataSource = dt;13                 this.reprss.DataBind();14             }15         }16 17 18 19         public void WriterRss()20         {21             string url = "http://feed.cnblogs.com/news/rss";22             XmlDocument document = new XmlDocument();23             document.Load(url);//24             XmlNodeList list = document.GetElementsByTagName("item");25             if (document.HasChildNodes)26             {27                 foreach (XmlNode xn in list)28                 {29                     if (xn.HasChildNodes)30                     {31 32                         NeoModel.RssReader rss = new NeoModel.RssReader();33                         XmlNodeList xlcd = xn.ChildNodes;34                         foreach (XmlNode xmlchild in xlcd)35                         {36                             switch (xmlchild.Name)//节点的限定名字37                             {38                                 case "title":39                                     rss.Title = xmlchild.InnerText;40                                     break;41                                 case "pubDate":42                                     rss.PubDate = Convert.ToDateTime(xmlchild.InnerText);43                                     break;44                                 case "guid":45                                     rss.Guid = xmlchild.InnerText;46                                     break;47                                 case "description":48                                     rss.Description = xmlchild.InnerText;49                                     break;50                                 default:51                                     break;52                             }53                         }54                         if (!bll.IsExistRssByGuid(rss.Guid))55                         {56                             bll.AddRssReader(rss);57                         }58                     }59                 }60             }61         }62     }63 }
RssReader
1 CREATE TABLE [dbo].[RssReader]( 2     [id] [int] IDENTITY(1,1) NOT NULL, 3     [title] [nvarchar](500) NULL, 4     [pubdate] [datetime] NULL, 5     [guid] [nvarchar](500) NULL, 6     [description] [ntext] NULL, 7  CONSTRAINT [PK_RssReader] PRIMARY KEY CLUSTERED  8 ( 9     [id] ASC10 )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]11 ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]12 13 GO

 

你可能感兴趣的文章
To be learned
查看>>
ActiveMQ
查看>>
rhel 7安装Mysql
查看>>
8、字符串操作
查看>>
React-Native获取文本框的值
查看>>
[导入]《WAP业务入门》培训材料
查看>>
Entity Framework
查看>>
简单数据结构总结——单调队列
查看>>
资源文件
查看>>
JS原型链与继承别再被问倒了
查看>>
SQL按照日、周、月、年、时间段统计数据
查看>>
通过全备+主从同步恢复被drop的库或表
查看>>
色环电阻的阻值识别
查看>>
面试TodoList
查看>>
我想参与TINY框架的构建,有什么要求没有?
查看>>
MySQL 加锁处理分析
查看>>
Linux下区分物理CPU、逻辑CPU和CPU核数
查看>>
optiontransferselect例子
查看>>
1053. 住房空置率 (20)
查看>>
《浪潮之巅》读后感
查看>>