简体   繁体   English

使用LINQ to XML读取RSS feed时出错

[英]Error Reading RSS Feed using LINQ to XML

In referencing this article I am receiving a NullReferenceException stating Object reference is not set to an instance of an object. 在引用本文时,我收到一个NullReferenceException说明Object reference is not set to an instance of an object. I'm not sure how to fix this solution as I've followed the steps in my reference article. 按照参考文章中的步骤操作,我不确定如何解决此解决方案。

Models 楷模

public class RssModel
{
    public string Title { get; set; }
    public string Link { get; set; }
    public string Description { get; set; }
    public string Image { get; set; }

}

public class ReadRssModel
{
    public static List<RssModel> GetRss()
    {
        var client = new WebClient();

        var xmlData = client.DownloadString("http://finance.yahoo.com/rss/headline?s=msft,goog,aapl");

        XDocument xml = XDocument.Parse(xmlData);

        var rssData = (from item in xml.Descendants("item")
                       select new RssModel
                       {
                           Title = ((string)item.Element("title")),
                           Link = ((string)item.Element("link")),
                           Description = ((string)item.Element("description")),

                           Image = ((string)item.Element("enclosure").Attribute("url"))
                       }).Take(20).ToList();

        return rssData;

    }
}

ViewModel 视图模型

public class RssViewModel
{
    public List<RssModel> RssFeed { get; set; }
}

Controller 控制者

 public class HomeController : Controller
{
    public ActionResult Index()
    {
        //return View();
        RssViewModel model = new RssViewModel();
        model.RssFeed = ReadRssModel.GetRss();
        return View(model);
    }
}

Index 指数

<div class="row">
<div class="col-md-8">
<h4>Feed</h4>

    @foreach (var item in Model.RssFeed)
    {
        @item.Title <br />
        @item.Description <br/>

    }

</div>

You have two layers of tags. 您有两层标签。 First the channel and then the items. 首先是渠道,然后是项目。 See code below. 请参见下面的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication47
{
    class Program
    {
        static void Main(string[] args)
        {
            XDocument xml = XDocument.Load("http://finance.yahoo.com/rss/headline?s=msft,goog,aapl");

            var results = xml.Descendants("channel").Select(x => new
            {
                Title = ((string)x.Element("title")),
                Link = ((string)x.Element("link")),
                Description = ((string)x.Element("description")),
                Image = ((string)x.Element("image").Element("url")),
                items = x.Elements("item").Take(20).Select(y => new {
                    title = (string)y.Element("title"),
                    link = (string)y.Element("link"),
                    description = (string)y.Element("description")
                }).ToList(),
            }).FirstOrDefault();

        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM