上次总结过rss的读取,现在总结ajax无刷新更新rss阅读
代码如下:
ashx代码:
ASHX
1 public class GetRss : IHttpHandler 2 { 3 4 public void ProcessRequest(HttpContext context) 5 { 6 context.Response.ContentType = "text/plain"; 7 string url = "http://rss.sina.com.cn/news/china/focus15.xml"; 8 XmlDocument document = new XmlDocument(); 9 document.Load(url); 10 XmlNodeList list = document.GetElementsByTagName("item"); 11 if (document.HasChildNodes) 12 { 13 DataTable dt = CreateXmlTable(); 14 foreach (XmlNode var in list) 15 { 16 if (var.HasChildNodes) 17 { 18 DataRow dr = dt.NewRow(); 19 XmlNodeList chidList = var.ChildNodes; 20 foreach (XmlNode v1 in chidList) 21 { 22 switch (v1.Name) 23 { 24 case "title": 25 dr["title"] = v1.InnerText; 26 break; 27 case "link": 28 dr["Link"] = v1.InnerText; 29 break; 30 case "author": 31 dr["author"] = v1.InnerText; 32 break; 33 case "category": 34 dr["summary"] = v1.InnerText; 35 break; 36 case "description": 37 dr["content"] = v1.InnerText; 38 break; 39 case "pubDate": 40 dr["published"] = v1.InnerText; 41 break; 42 default: 43 break; 44 } 45 } 46 dt.Rows.Add(dr); 47 } 48 } 49 WriteToSQL(dt); 50 } 51 string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; 52 DataSet ds = new DataSet(); 53 using (SqlConnection con = new SqlConnection(connectionString)) 54 { 55 SqlCommand cmd = con.CreateCommand(); 56 cmd.CommandText = "SELECT top 10 * FROM RSS WHERE summary='国内要闻' order by [published] desc"; 57 SqlDataAdapter da = new SqlDataAdapter(cmd); 58 59 da.Fill(ds); 60 } 61 string html = ""; 62 if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0) 63 { 64 html = ""; 65 } 66 for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 67 { 68 html = html + "<div><div>"; 69 html = html + "<a href='" + ds.Tables[0].Rows[i]["link"].ToString() + "' target='_blank'>" + ds.Tables[0].Rows[i]["title"].ToString() + "</a><br/><br/></div><div>" + ds.Tables[0].Rows[i]["content"].ToString() + "</div></div><hr>"; 70 } 71 context.Response.Write(html); 72 } 73 public void WriteToSQL(DataTable dt) 74 { 75 76 if (dt == null || dt.Rows.Count <= 0) 77 { 78 return; 79 } 80 for (int i = 0; i < dt.Rows.Count; i++) 81 { 82 RssMod model = new RssMod(); 83 model.Link = dt.Rows[i]["link"].ToString(); 84 model.Author = dt.Rows[i]["author"].ToString(); 85 model.Content = dt.Rows[i]["content"].ToString(); 86 model.Published = Convert.ToDateTime(dt.Rows[i]["published"].ToString()); 87 model.Summary = dt.Rows[i]["summary"].ToString(); 88 model.Title = dt.Rows[i]["title"].ToString(); 89 if (!IsExistsByLink(model.Link)) 90 { 91 ExcuteNonQuery(model); 92 } 93 } 94 } 95 public void ExcuteNonQuery(RssMod model) 96 { 97 string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; 98 using (System.Data.SqlClient.SqlConnection con = new SqlConnection(connectionString)) 99 { 100 con.Open(); 101 System.Data.SqlClient.SqlCommand cmd = con.CreateCommand(); 102 cmd.CommandText = "INSERT INTO Rss([link],[title],[summary],[author],[content],[published]) VALUES(@link,@title,@summary,@author,@content,@published)"; 103 SqlParameter[] sp ={ 104 new SqlParameter("@link",SqlDbType.NVarChar,300), 105 new SqlParameter("@title",SqlDbType.NVarChar,200), 106 new System.Data.SqlClient.SqlParameter("@summary",SqlDbType.NVarChar,4000), 107 new SqlParameter("@author",SqlDbType.NVarChar,50), 108 new SqlParameter("@content",SqlDbType.NVarChar,4000), 109 new SqlParameter("@published",SqlDbType.DateTime) 110 }; 111 sp[0].Value = model.Link; 112 sp[1].Value = model.Title; 113 sp[2].Value = model.Summary; 114 sp[3].Value = model.Author; 115 sp[4].Value = model.Content; 116 sp[5].Value = Convert.ToDateTime(model.Published); 117 cmd.Parameters.AddRange(sp); 118 cmd.ExecuteNonQuery(); 119 } 120 } 121 public bool IsExistsByLink(string link) 122 { 123 string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Conn"].ConnectionString.ToString(); 124 using (SqlConnection con = new SqlConnection(ConnectionString)) 125 { 126 con.Open(); 127 string strSql = String.Format("SELECT COUNT(0) FROM RSS WHERE LINK='{0}'", link); 128 SqlCommand cmd = con.CreateCommand(); 129 cmd.CommandText = strSql; 130 object ob = cmd.ExecuteScalar(); 131 return Convert.ToInt32(ob) > 0; 132 } 133 } 134 public DataTable CreateXmlTable() 135 { 136 DataTable dt = new DataTable(); 137 dt.Columns.Add("Link", typeof(string)); 138 dt.Columns.Add("title", typeof(string)); 139 dt.Columns.Add("summary", typeof(string)); 140 dt.Columns.Add("author", typeof(string)); 141 dt.Columns.Add("content", typeof(string)); 142 dt.Columns.Add("published", typeof(string)); 143 return dt; 144 } 145 146 public bool IsReusable 147 { 148 get 149 { 150 return false; 151 } 152 } 153 154 } 155 public class RssMod 156 { 157 public RssMod() 158 { 159 } 160 private string link; 161 public string Link 162 { 163 get { return link; } 164 set { link = value; } 165 } 166 167 private string title; 168 public string Title 169 { 170 get { return title; } 171 set { title = value; } 172 } 173 174 private string summary; 175 public string Summary 176 { 177 get { return summary; } 178 set { summary = value; } 179 } 180 181 private string author; 182 public string Author 183 { 184 get { return author; } 185 set { author = value; } 186 } 187 188 private string content; 189 public string Content 190 { 191 get { return content; } 192 set { content = value; } 193 } 194 195 private DateTime published; 196 public DateTime Published 197 { 198 get { return published; } 199 set { published = value; } 200 } 201 }
页面代码:
页面代码
1 <head runat="server"> 2 <title>无标题页</title> 3 4 <script src="js/jquery-1.4.2.min.js" type="text/javascript"></script> 5 6 <script language="javascript" type="text/javascript"> 7 window.setInterval('GetXml()',5000); 8 function GetXml(){ 9 $.get("GetRss.ashx",function(data){ 10 $("#rssItem").html(data); 11 }); 12 } 13 </script> 14 15 </head> 16 <body > 17 <form id="form1" runat="server" method="post"> 18 <div align="center"> 19 <table cellpadding="0" cellspacing="0" border="0" width="80%"> 20 <tr> 21 <td> 22 <div id="rssItem" style="width: 80%;"> 23 </div> 24 </td> 25 </tr> 26 </table> 27 </div> 28 </form> 29 </body> 30 </html>
怀揣着一点点梦想的年轻人
相信技术和创新的力量
喜欢快速反应的工作节奏
相信技术和创新的力量
喜欢快速反应的工作节奏