有时候会遇见要读取网站提供的rss,这时候需要用到XmlDocument类来实现。
以博客园为例,我的博客的rss地址是http://www.cnblogs.com/hfliyi/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 RssModel 150 { 151 public RssModel() 152 { 153 } 154 private string link; 155 public string Link 156 { 157 get { return link; } 158 set { link = value; } 159 } 160 161 private string title; 162 public string Title 163 { 164 get { return title; } 165 set { title = value; } 166 } 167 168 private string summary; 169 public string Summary 170 { 171 get { return summary; } 172 set { summary = value; } 173 } 174 175 private string author; 176 public string Author 177 { 178 get { return author; } 179 set { author = value; } 180 } 181 182 private string content; 183 public string Content 184 { 185 get { return content; } 186 set { content = value; } 187 } 188 189 private DateTime published; 190 public DateTime Published 191 { 192 get { return published; } 193 set { published = value; } 194 } 195 }
前台源代码:
前台原代码
1 <asp:GridView ID="gv_state" runat="server" GridLines="None" BorderWidth="0px" CellPadding="0" 2 CellSpacing="1" align="center" AutoGenerateColumns="false"> 3 <Columns> 4 <asp:TemplateField HeaderText="文章标题"> 5 <ItemTemplate> 6 <a href='<%#Eval("link") %>' target="_blank"> 7 <%#Eval("title")%> 8 </a> 9 </ItemTemplate> 10 <ItemStyle Width="20%" /> 11 </asp:TemplateField> 12 <asp:TemplateField HeaderText="作者"> 13 <ItemTemplate> 14 <%#((System.Data.DataRowView)Container.DataItem)["author"]%> 15 </ItemTemplate> 16 <ItemStyle Width="20%" /> 17 </asp:TemplateField> 18 <asp:TemplateField HeaderText="发布日期"> 19 <ItemTemplate> 20 <%#((System.Data.DataRowView)Container.DataItem)["published"]%> 21 </ItemTemplate> 22 <ItemStyle Width="20%" /> 23 </asp:TemplateField> 24 <asp:TemplateField HeaderText="摘要"> 25 <ItemTemplate> 26 <%#Eval("summary").ToString().Length>20?Eval("summary").ToString().Substring(0,20):Eval("summary")%> 27 </ItemTemplate> 28 <ItemStyle Width="20%" /> 29 </asp:TemplateField> 30 </Columns> 31 <RowStyle CssClass="tr3" Font-Size="12px" Height="28px" /> 32 <HeaderStyle CssClass="itable_title" /> 33 <EmptyDataTemplate> 34 <tr class="itable_title"> 35 <th width="20%"> 36 文章标题 37 </th> 38 <th width="20%"> 39 作者 40 </th> 41 <th width="20%"> 42 发布日期 43 </th> 44 <th width="20%"> 45 摘要 46 </th> 47 </tr> 48 <tr class="tr3"> 49 <td class="grid_no_result" colspan="4"> 50 <span>当前没有查询记录</span> 51 </td> 52 </tr> 53 </EmptyDataTemplate> 54 </asp:GridView>
例如读取新浪财经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 RssMod 146 { 147 public RssMod() 148 { 149 } 150 private string link; 151 public string Link 152 { 153 get { return link; } 154 set { link = value; } 155 } 156 157 private string title; 158 public string Title 159 { 160 get { return title; } 161 set { title = value; } 162 } 163 164 private string summary; 165 public string Summary 166 { 167 get { return summary; } 168 set { summary = value; } 169 } 170 171 private string author; 172 public string Author 173 { 174 get { return author; } 175 set { author = value; } 176 } 177 178 private string content; 179 public string Content 180 { 181 get { return content; } 182 set { content = value; } 183 } 184 185 private DateTime published; 186 public DateTime Published 187 { 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 ID 10 { 11 get { return _id; } 12 set { _id = value; } 13 } 14 15 private string _title; 16 public string Title 17 { 18 get { return _title; } 19 set { _title = value; } 20 } 21 22 private DateTime? _pubdate; 23 public DateTime? PubDate 24 { 25 get { return _pubdate; } 26 set { _pubdate = value; } 27 } 28 29 private string _guid; 30 public string Guid 31 { 32 get { return _guid; } 33 set { _guid = value; } 34 } 35 36 private string _description; 37 public string Description 38 { 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 /// <summary> 9 /// 添加rss 10 /// </summary> 11 /// <param name="rss"></param> 12 /// <returns></returns> 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 /// <summary> 26 /// 判断rss是否存在 27 /// </summary> 28 /// <param name="guid"></param> 29 /// <returns></returns> 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 /// <summary> 39 /// 40 /// </summary> 41 /// <returns></returns> 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 /// <summary> 9 /// 10 /// </summary> 11 /// <param name="rss"></param> 12 /// <returns></returns> 13 public bool AddRssReader(NeoModel.RssReader rss) 14 { 15 return dal.AddRssReader(rss); 16 } 17 /// <summary> 18 /// 19 /// </summary> 20 /// <param name="guid"></param> 21 /// <returns></returns> 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 <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 2 <style type="text/css"> 3 .title 4 { 5 font-size: 11pt; 6 margin: 5px 0; 7 background: transparent url(../images/icon.rss.current.png) no-repeat 4px 50%; 8 padding-left: 28px; 9 word-break: break-word; 10 -webkit-user-select: text; 11 } 12 h3, h4 13 { 14 padding: 0; 15 } 16 a 17 { 18 color: #036; 19 } 20 h3 21 { 22 display: block; 23 -webkit-margin-before: 1em; 24 -webkit-margin-after: 1em; 25 -webkit-margin-start: 0px; 26 -webkit-margin-end: 0px; 27 font-weight: bold; 28 } 29 h4 30 { 31 display: block; 32 -webkit-margin-before: 1.33em; 33 -webkit-margin-after: 1.33em; 34 -webkit-margin-start: 0px; 35 -webkit-margin-end: 0px; 36 } 37 .updated 38 { 39 color: #666; 40 font-weight: normal; 41 padding-left:8px; 42 margin: 5px 0; 43 } 44 .description 45 { 46 margin: 14px 20px 20px 0; 47 padding-left:8px; 48 max-width: 850px; 49 line-height: 1.8; 50 font-size: 10pt; 51 word-break: break-word; 52 } 53 .actions 54 { 55 padding: 5px 8px; 56 background-color: #f4f5f5; 57 border-radius: 3px; 58 width:99%; 59 } 60 div 61 { 62 display: block; 63 width: 100%; 64 } 65 body 66 { 67 color: Black; 68 cursor: default; 69 font-size: 9pt; 70 font-family: Segoe UI, 微软雅黑 , Tahoma; 71 } 72 </style> 73 </asp:Content> 74 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 75 <asp:Repeater ID="reprss" runat="server"> 76 <ItemTemplate> 77 <div style=" margin:10px; padding:5px; background-color:White; border:1px #ccc solid; border-radius:4px;"> 78 <div> 79 <h3 class="title"> 80 <a href="<%#Eval("guid") %>" target="_blank"> 81 <%#Eval("title") %></a></h3> 82 <h4 class="updated"> 83 <%#Eval("pubdate")%> 84 </h4> 85 </div> 86 <div class="description"> 87 <%#Eval("description")%><br /> 88 89 </div> 90 <div class="actions"> 91 <a href="<%#Eval("guid") %>" target="_blank">打开页面</a> 92 </div> 93 </div> 94 </ItemTemplate> 95 </asp:Repeater> 96 </asp:Content>
后台
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] ASC 10 )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
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
相信技术和创新的力量
喜欢快速反应的工作节奏