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

.cs

/// <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;

            }
 readonly string _cnblogRssPath = ConfigurationManager.AppSettings["cnblog-news"];
<appSettings>

        <!--博客园新闻rss地址-->

        <add key="cnblog-news" value="http://feed.cnblogs.com/news/rss"/>

    </appSettings>
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; }

    }

}

js

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);

}

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

你可能感兴趣的:(rss)