博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用Ajax无刷新更新rss阅读
阅读量:7232 次
发布时间:2019-06-29

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

上次总结过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 + "
" + ds.Tables[0].Rows[i]["content"].ToString() + "

"; 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 IsReusable147 {148 get149 {150 return false;151 }152 }153 154 }155 public class RssMod156 {157 public RssMod()158 {159 }160 private string link;161 public string Link162 {163 get { return link; }164 set { link = value; }165 }166 167 private string title;168 public string Title169 {170 get { return title; }171 set { title = value; }172 }173 174 private string summary;175 public string Summary176 {177 get { return summary; }178 set { summary = value; }179 }180 181 private string author;182 public string Author183 {184 get { return author; }185 set { author = value; }186 }187 188 private string content;189 public string Content190 {191 get { return content; }192 set { content = value; }193 }194 195 private DateTime published;196 public DateTime Published197 {198 get { return published; }199 set { published = value; }200 }201 }

页面代码:

页面代码
1  2     无标题页 3  4      5  6     14 15 16 17     
18
19
20
21
25
26
22
23
24
27
28
29 30

 

转载地址:http://umsfm.baihongyu.com/

你可能感兴趣的文章
maven3常用命令、java项目搭建、web项目搭建
查看>>
BZOJ1023:[SHOI2008]仙人掌图——题解
查看>>
JavaBean简单及使用
查看>>
css3基本属性
查看>>
PEB结构学习
查看>>
python之属性描述符与属性查找规则
查看>>
linux之各目录作用
查看>>
海淀、西城、东城各类小学排名(转载)
查看>>
AbstractFactory抽象工厂模式(创建型模式)
查看>>
Flex3 + Spring配置
查看>>
Android牟利之道(四)--如何推广你的产品,即你的APP
查看>>
Android的各种Drawable讲解
查看>>
java学习笔记9.20
查看>>
如何防止在同一台机器上重复登录(WinForm程序)
查看>>
使用Fiddler调试Wcf Rest
查看>>
XSLD 简单小例子
查看>>
数组去重
查看>>
安装和部署Jenkins
查看>>
cs231n 17-18 assignment2 出现 No module named 'past' 解决方法
查看>>
Ajax基础
查看>>