首页 > 其他 > 详细

C# 读取 博客园 Rss 的新闻数据

时间:2014-02-19 23:59:38      阅读:706      评论:0      收藏:0      [点我收藏+]

.cs

bubuko.com,布布扣
/// <summary>
        /// 获取博客园新闻Rss数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetRssXmlDoc()
        {
            XmlDocument doc = new XmlDocument();
            IList<RssXml> rssXmls = new List<RssXml>();
            try
            {
                //加载Xml文件  
                doc.Load(_cnblogRssPath);
                //根结点
                XmlNode dataTableSettingsNode = doc.SelectSingleNode("rss");
                if (dataTableSettingsNode != null)
                {
                    XmlNode dataCellsNode = dataTableSettingsNode.SelectSingleNode("channel");

                    if (dataCellsNode != null)
                    {
                        XmlNodeList dataCellNode = dataCellsNode.SelectNodes("item");
                      
                        if (dataCellNode != null)
                            foreach (XmlNode node in dataCellNode)
                            {
                                RssXml rssXml = new RssXml();
                                var title = node.SelectSingleNode("title");
                                var link = node.SelectSingleNode("link");
                                var pubdate = node.SelectSingleNode("pubdate");
                                var guid = node.SelectSingleNode("guid");
                                var description = node.SelectSingleNode("description");
                                if (title != null)
                                    rssXml.Title = title.InnerText;
                                if (link != null)
                                    rssXml.Link = link.InnerText;
                                if (pubdate != null)
                                    rssXml.PubDate = pubdate.InnerText;
                                if (guid != null)
                                    rssXml.Guid = guid.InnerText;
                                if (description != null)
                                    rssXml.Description = description.InnerText;
                                rssXmls.Add(rssXml);
                            }
                    }
                }
                var zwJson = new ZwJson
                {
                    Data = rssXmls,
                    IsSuccess = true,
                    JsExecuteMethod = "ajax_GetRssXmlDoc"
                };
                var json = Json(zwJson, JsonRequestBehavior.AllowGet);
                return json;
            }
            catch (Exception)
            {
                throw;
            }
bubuko.com,布布扣
 readonly string _cnblogRssPath = ConfigurationManager.AppSettings["cnblog-news"];
<appSettings>
        <!--博客园新闻rss地址-->
        <add key="cnblog-news" value="http://feed.cnblogs.com/news/rss"/>
    </appSettings>
bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyMvc4Project.Infrastructure.Rss
{
    public class RssXml
    {
        /// <summary>
        /// 主题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 链接
        /// </summary>
        public string Link { get; set; }

        /// <summary>
        /// 日期
        /// </summary>
        public string PubDate { get; set; }

        /// <summary>
        /// guid
        /// </summary>
        public string Guid { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public string Description { get; set; }
    }
}
bubuko.com,布布扣

js

bubuko.com,布布扣
function ajax_GetRssXmlDoc(data) {
    var html = "";
    var len = data.Data.length;
    $(".panel-join .panel-body").empty();
    html += ‘<ul>‘;
    for (var i = 0; i < 10; i++) {
        html += ‘<li><a href="‘ + data.Data[i].Link + ‘" title="‘ + data.Data[i].Title + ‘">‘ 
            + data.Data[i].Title.substr(0, 19) + ‘</a></li>‘;
    }

    html += ‘</ul>‘;
    $(".panel-join .panel-body").append(html);
}
bubuko.com,布布扣

bubuko.com,布布扣

C# 读取 博客园 Rss 的新闻数据

原文:http://www.cnblogs.com/zhangwei595806165/p/3555900.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!