读取博客园rss

 Model

  
  
  
  
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5.  
  6. namespace NeoModel 
  7.     public class RssReader 
  8.     { 
  9.         public RssReader() 
  10.         { 
  11.         } 
  12.         private int _id; 
  13.         public int ID 
  14.         { 
  15.             get { return _id; } 
  16.             set { _id = value; } 
  17.         } 
  18.  
  19.         private string _title; 
  20.         public string Title 
  21.         { 
  22.             get { return _title; } 
  23.             set { _title = value; } 
  24.         } 
  25.  
  26.         private DateTime? _pubdate; 
  27.         public DateTime? PubDate 
  28.         { 
  29.             get { return _pubdate; } 
  30.             set { _pubdate = value; } 
  31.         } 
  32.  
  33.         private string _guid; 
  34.         public string Guid 
  35.         { 
  36.             get { return _guid; } 
  37.             set { _guid = value; } 
  38.         } 
  39.  
  40.         private string _description; 
  41.         public string Description 
  42.         { 
  43.             get { return _description; } 
  44.             set { _description = value; } 
  45.         } 
  46.     } 

DAL

 

  
  
  
  
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Data; 
  6. using System.Data.SqlClient; 
  7. using NeoCommon; 
  8. namespace NeoDAL 
  9.     public class RssReaderDal 
  10.     { 
  11.         public RssReaderDal() 
  12.         { 
  13.         } 
  14.         /// <summary> 
  15.         /// 添加rss 
  16.         /// </summary> 
  17.         /// <param name="rss"></param> 
  18.         /// <returns></returns> 
  19.         public bool AddRssReader(NeoModel.RssReader rss) 
  20.         { 
  21.             string strSql = "Insert into RssReader(title,pubdate,[guid],[description]) values(@title,@pubdate,@guid,@description)"
  22.             SqlParameter[] sp = {  
  23.                                new SqlParameter("@title",rss.Title), 
  24.                                new SqlParameter("@pubdate",rss.PubDate), 
  25.                                new SqlParameter("@guid",rss.Guid), 
  26.                                new SqlParameter("@description",rss.Description) 
  27.                                }; 
  28.             return DbHelperSQL.ExecuteSql(strSql, sp) > 0; 
  29.  
  30.         } 
  31.         /// <summary> 
  32.         /// 判断rss是否存在 
  33.         /// </summary> 
  34.         /// <param name="guid"></param> 
  35.         /// <returns></returns> 
  36.         public bool IsExistRssByGuid(string guid) 
  37.         { 
  38.             string strSql = "SELECT COUNT(0) FROM RssReader WHERE [guid]=@guid"
  39.             SqlParameter[] sp = {  
  40.                                 new SqlParameter("@guid",guid) 
  41.                                 }; 
  42.             return Convert.ToInt32(DbHelperSQL.GetSingle(strSql, sp)) > 0; 
  43.         } 
  44.         /// <summary> 
  45.         ///  
  46.         /// </summary> 
  47.         /// <returns></returns> 
  48.         public DataTable GetRss() 
  49.         { 
  50.             string strSql = "Select * from RssReader order by pubdate desc"
  51.             DataSet ds = DbHelperSQL.Query(strSql); 
  52.             if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0) 
  53.             { 
  54.                 return new DataTable(); 
  55.             } 
  56.             return ds.Tables[0]; 
  57.         } 
  58.  
  59.     } 

BLL

 

  
  
  
  
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5. using System.Data; 
  6.  
  7. namespace NeoBLL 
  8.     public class RssReader 
  9.     { 
  10.         NeoDAL.RssReaderDal dal = new NeoDAL.RssReaderDal(); 
  11.         public RssReader() { } 
  12.  
  13.         /// <summary> 
  14.         ///  
  15.         /// </summary> 
  16.         /// <param name="rss"></param> 
  17.         /// <returns></returns> 
  18.         public bool AddRssReader(NeoModel.RssReader rss) 
  19.         { 
  20.             return dal.AddRssReader(rss); 
  21.         } 
  22.         /// <summary> 
  23.         ///  
  24.         /// </summary> 
  25.         /// <param name="guid"></param> 
  26.         /// <returns></returns> 
  27.         public bool IsExistRssByGuid(string guid) 
  28.         { 
  29.             return dal.IsExistRssByGuid(guid); 
  30.         } 
  31.         public DataTable GetRss() 
  32.         { 
  33.             return dal.GetRss(); 
  34.         } 
  35.     } 

页面源

 

  
  
  
  
  1. <%@ Page Title="" Language="C#" MasterPageFile="~/master/Oa.Master" AutoEventWireup="true" 
  2.     CodeBehind="RssReader.aspx.cs" Inherits="NeoBackground.Baidu.RssReader" %> 
  3.  
  4. <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
  5.     <style type="text/css"> 
  6.         .title 
  7.         { 
  8.             font-size: 11pt; 
  9.             margin: 5px 0; 
  10.             background: transparent url(../images/icon.rss.current.png) no-repeat 4px 50%; 
  11.             padding-left: 28px; 
  12.             word-break: break-word; 
  13.             -webkit-user-select: text; 
  14.         } 
  15.         h3, h4 
  16.         { 
  17.             padding: 0; 
  18.         } 
  19.         a 
  20.         { 
  21.             color: #036; 
  22.         } 
  23.         h3 
  24.         { 
  25.             display: block; 
  26.             -webkit-margin-before: 1em; 
  27.             -webkit-margin-after: 1em; 
  28.             -webkit-margin-start: 0px; 
  29.             -webkit-margin-end: 0px; 
  30.             font-weight: bold; 
  31.         } 
  32.         h4 
  33.         { 
  34.             display: block; 
  35.             -webkit-margin-before: 1.33em; 
  36.             -webkit-margin-after: 1.33em; 
  37.             -webkit-margin-start: 0px; 
  38.             -webkit-margin-end: 0px; 
  39.         } 
  40.         .updated 
  41.         { 
  42.             color: #666; 
  43.             font-weight: normal; 
  44.              padding-left:8px; 
  45.             margin: 5px 0; 
  46.         } 
  47.         .description 
  48.         { 
  49.             margin: 14px 20px 20px 0; 
  50.             padding-left:8px; 
  51.             max-width: 850px; 
  52.             line-height: 1.8; 
  53.             font-size: 10pt; 
  54.             word-break: break-word; 
  55.         } 
  56.         .actions 
  57.         { 
  58.             padding: 5px 8px; 
  59.             background-color: #f4f5f5; 
  60.             border-radius: 3px; 
  61.              width:99%; 
  62.         } 
  63.         div 
  64.         { 
  65.             display: block; 
  66.             width: 100%; 
  67.         } 
  68.         body 
  69.         { 
  70.             color: Black; 
  71.             cursor: default; 
  72.             font-size: 9pt; 
  73.             font-family: Segoe UI, 微软雅黑 , Tahoma; 
  74.         } 
  75.     </style> 
  76. </asp:Content> 
  77. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
  78.     <asp:Repeater ID="reprss" runat="server"> 
  79.         <ItemTemplate> 
  80.             <div style=" margin:10px; padding:5px; background-color:White; border:1px #ccc solid; border-radius:4px;"> 
  81.                 <div> 
  82.                     <h3 class="title"> 
  83.                         <a href="<%#Eval("guid") %>" target="_blank"> 
  84.                             <%#Eval("title") %></a></h3> 
  85.                     <h4 class="updated"> 
  86.                         <%#Eval("pubdate")%> 
  87.                     </h4> 
  88.                 </div> 
  89.                 <div class="description"> 
  90.                     <%#Eval("description")%><br /> 
  91.                    
  92.                 </div> 
  93.                 <div class="actions"> 
  94.                     <a href="<%#Eval("guid") %>"  target="_blank">打开页面</a> 
  95.                 </div> 
  96.             </div> 
  97.         </ItemTemplate> 
  98.     </asp:Repeater> 
  99. </asp:Content> 

后台

 

  
  
  
  
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Web; 
  5. using System.Web.UI; 
  6. using System.Web.UI.WebControls; 
  7. using System.Xml; 
  8. using System.Data; 
  9. namespace NeoBackground.Baidu 
  10.     public partial class RssReader : System.Web.UI.Page 
  11.     { 
  12.         NeoBLL.RssReader bll = new NeoBLL.RssReader(); 
  13.         protected void Page_Load(object sender, EventArgs e) 
  14.         { 
  15.             if (!IsPostBack) 
  16.             { 
  17.                 // WriterRss(); 
  18.                 DataTable dt = bll.GetRss(); 
  19.                 this.reprss.DataSource = dt; 
  20.                 this.reprss.DataBind(); 
  21.             } 
  22.         } 
  23.  
  24.  
  25.  
  26.         public void WriterRss() 
  27.         { 
  28.             string url = "http://feed.cnblogs.com/news/rss"
  29.             XmlDocument document = new XmlDocument(); 
  30.             document.Load(url);// 
  31.             XmlNodeList list = document.GetElementsByTagName("item"); 
  32.             if (document.HasChildNodes) 
  33.             { 
  34.                 foreach (XmlNode xn in list) 
  35.                 { 
  36.                     if (xn.HasChildNodes) 
  37.                     { 
  38.  
  39.                         NeoModel.RssReader rss = new NeoModel.RssReader(); 
  40.                         XmlNodeList xlcd = xn.ChildNodes; 
  41.                         foreach (XmlNode xmlchild in xlcd) 
  42.                         { 
  43.                             switch (xmlchild.Name)//节点的限定名字 
  44.                             { 
  45.                                 case "title"
  46.                                     rss.Title = xmlchild.InnerText; 
  47.                                     break
  48.                                 case "pubDate"
  49.                                     rss.PubDate = Convert.ToDateTime(xmlchild.InnerText); 
  50.                                     break
  51.                                 case "guid"
  52.                                     rss.Guid = xmlchild.InnerText; 
  53.                                     break
  54.                                 case "description"
  55.                                     rss.Description = xmlchild.InnerText; 
  56.                                     break
  57.                                 default
  58.                                     break
  59.                             } 
  60.                         } 
  61.                         if (!bll.IsExistRssByGuid(rss.Guid)) 
  62.                         { 
  63.                             bll.AddRssReader(rss); 
  64.                         } 
  65.                     } 
  66.                 } 
  67.             } 
  68.         } 
  69.     } 

显示如下图:

 

 

你可能感兴趣的:(xml,rss,C#)