c# - 使用 MVC 和 Linq to XML 读取多个 RSS 提要

标签 c# asp.net-mvc asp.net-mvc-3 linq-to-xml

在编码方面我真的很生疏,一直在努力寻找答案,但现在我已经筋疲力尽了。

我的网站有一个存储 RSS URL 的数据库。对于我的生活,我无法弄清楚如何遍历并显示所有 RSS 提要数据。

我可以让一个 Feed 显示得很好,但是要显示所有这些,我只是不知道如何才能让它们全部显示。任何帮助将不胜感激!

模型:RSS.cs

namespace MyWebsite.Models
{
    public class RSS
    {
        public string Link    { get; set; }
        public string Title         { get; set; }
        public string Description   { get; set; }
        public string PubDate       { get; set; }
        public string Date          { get; set; }
    }
}

模型:RSSReader.cs

namespace MyWebsite.Models
{
    public class RSSReader
    {
        private static string _URL = "";

        public static IEnumerable<RSS> GetRSSFeed()
        {
            IEnumerable<RSS> feeds = null;
            XDocument feedXml = null;
            XNamespace rss = "http://purl.org/rss/1.0/";
            XNamespace dc = "http://purl.org/dc/elements/1.1/";
            int count = 0;

            foreach (var url in BansheeData.GetURL(count))
            {
                _URL = url.url1;
                feedXml = XDocument.Load(_URL);

                    feeds = from feed in feedXml.Descendants(rss + "item")
                            select new RSS
                            {
                                Title = feed.Element(rss + "title").Value,
                                Link = feed.Element(rss + "link").Value,
                                Date = feed.Element(dc + "date").Value.Remove(10)
                            };

                count++;
            }

            return feeds;
        }
    }
}

Controller :RSSController.cs

public ActionResult Index()
{
      var urlFeed = MyWebsite.Models.RSSReader.GetRSSFeed();
      return View(urlFeed);
}

查看: _RSSFeed.cshtml

@model IEnumerable<MyWebsite.Models.RSS>

@foreach (var item in Model) { 
    <section>
        <header class="overflow">
            <h1>
                <a href="@item.Link" target="_blank" class="alt overflow">
                    <span class="fl">@Html.Raw(item.Title)</span>
                    <span class="ref fr">Source, @Html.Encode(item.Date)</span>
                </a>
            </h1>
        </header>
    </section>
}

最佳答案

我想我在您的代码中发现了一个问题。您缺少为每次迭代将项目添加到列表中。

所以代替:

            foreach (var url in BansheeData.GetURL(count))
            {
                _URL = url.url1;
                feedXml = XDocument.Load(_URL);

                feeds = from feed in feedXml.Descendants(rss + "item")
                            select new RSS
                            {
                                Title = feed.Element(rss + "title").Value,
                                Link = feed.Element(rss + "link").Value,
                                Date = feed.Element(dc + "date").Value.Remove(10)
                            };

                count++;
            }

            return feeds;

您尝试在每次迭代中将 AddRange 放入列表中:

        var rssList = new List<RSS>;

        foreach (var url in BansheeData.GetURL(count))
        {
            _URL = url.url1;
            feedXml = XDocument.Load(_URL);

            feeds = from feed in feedXml.Descendants(rss + "item")
                        select new RSS
                        {
                            Title = feed.Element(rss + "title").Value,
                            Link = feed.Element(rss + "link").Value,
                            Date = feed.Element(dc + "date").Value.Remove(10)
                        };

            rssList.AddRange(feeds);

            count++;
        }

        return rssList;

这应该有所帮助。现在您应该得到 RSS 的完整列表。核心逻辑是当您进行迭代并在每个迭代周期中获得所需的项目时,您应该记住它们。这意味着将它们保存在临时列表中。

关于c# - 使用 MVC 和 Linq to XML 读取多个 RSS 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15289693/

相关文章:

c# - 如何修复 WinForms 中的组件布局行为

c# - 制表符分隔的txt文件从字符串数组中读取int值

c# - WiX 3.8 : Two MSI using the same registry values. 如何仅在卸载两个 MSI 的情况下删除注册表值?

c# - 无法加载文件或程序集 SMDiagnostics.dll

asp.net - Html.AntiForgeryToken 辅助函数有什么用?

c# - 将文件上传到c#中的网络映射驱动器

c# - 在 ASP.NET MVC 中使用临时表

c# - 将值添加到动态填充的列表 c#

css - MVC : which CSS class implements Html. EditorFor() 方法?

asp.net-mvc - 使用 EF POCO 类作为 MVC 2 模型(带有数据注释)