【问题标题】:Right way to work with RSS in ASP.NET Core 1.0 RC2在 ASP.NET Core 1.0 RC2 中使用 RSS 的正确方法
【发布时间】:2016-05-18 20:23:41
【问题描述】:

在 C# 中使用 ASP.Net Core 1.0 (RC2) 从 RSS 提要获取数据的正确/最佳方法是什么。

我想处理来自我的 Wordpress 博客 https://blogs.msdn.microsoft.com/martinkearn/feed/ 的 RSS 提要中的数据

我知道在 ASP.net 4.x 中,您会使用 RssReaderSyndicationFeed,但我找不到 ASP.net 核心的等效项。

据我所知,它返回原始提要,但我不知道如何从中提取数据。我想枚举项目并从每个项目中获取titledescription

    var feedUrl = "https://blogs.msdn.microsoft.com/martinkearn/feed/";
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(feedUrl);
        var responseMessage = await client.GetAsync(feedUrl);
        var responseString = await responseMessage.Content.ReadAsStringAsync();
    }

【问题讨论】:

    标签: rss asp.net-core asp.net-core-mvc asp.net-core-1.0


    【解决方案1】:

    为了完整起见,我将包含最终代码,它是示例@Sock 的精简版本,链接到答案的“构建您自己的 XML 解析器”部分。 @Sock 的答案仍然是最完整的答案,但这个示例应该对任何寻找 ASP.NET Core 的快速、简单代码示例的人有用。

    型号

    public class FeedItem
    {
        public string Link { get; set; } 
        public string Title { get; set; } 
        public string Content { get; set; } 
        public DateTime PublishDate { get; set; } 
    }
    

    控制器

    public class ArticlesController : Controller
    {
        public async Task<IActionResult> Index()
        {
            var articles = new List<FeedItem>();
            var feedUrl = "https://blogs.msdn.microsoft.com/martinkearn/feed/";
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(feedUrl);
                var responseMessage = await client.GetAsync(feedUrl);
                var responseString = await responseMessage.Content.ReadAsStringAsync();
    
                //extract feed items
                XDocument doc = XDocument.Parse(responseString);
                var feedItems = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i => i.Name.LocalName == "item")
                                select new FeedItem
                                {
                                    Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
                                    Link = item.Elements().First(i => i.Name.LocalName == "link").Value,
                                    PublishDate = ParseDate(item.Elements().First(i => i.Name.LocalName == "pubDate").Value),
                                    Title = item.Elements().First(i => i.Name.LocalName == "title").Value
                                };
                articles = feedItems.ToList();
            }
    
            return View(articles);
        }
    
        private DateTime ParseDate(string date)
        {
            DateTime result;
            if (DateTime.TryParse(date, out result))
                return result;
            else
                return DateTime.MinValue;
        }
    }
    

    【讨论】:

      【解决方案2】:

      根据this issue,System.ServiceModel.Syndication 尚未移植到 ASP.NET Core。目前,这为您提供了 2 个选项:

      • 以完整的 .NET 框架为目标,以提供对 SyndicationFeed 的访问权限
      • 使用 .NET Core 构建您自己的 XML 解析器以重现您需要的功能

      以完整的 .NET 框架为目标

      这无疑是最简单的方法,具体取决于您的要求。

      如果您只部署到 Windows,那么您可以在 .NET 4.X 框架之上运行 ASP.NET Core。为此,请使用类似的方式更新您的 project.json

      frameworks": {
        "netcoreapp1.0": {
          "imports": [
            "dotnet5.6",
            "dnxcore50",
            "portable-net45+win8"
          ]
        }
      }
      

      对此:

      frameworks": {
        "net452": {
           "frameworkAssemblies": {
               "System.ServiceModel": ""
           }
         }
      }
      

      构建您自己的 XML 解析器

      这将为您提供最大的灵活性,因为您仍然可以使用 .NET Core 框架跨平台运行。反序列化您已经获得的字符串需要更多的工作,但是有很多关于如何做到这一点的示例,例如http://www.anotherchris.net/csharp/simplified-csharp-atom-and-rss-feed-parser/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-13
        • 1970-01-01
        • 2016-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-18
        • 1970-01-01
        相关资源
        最近更新 更多