在ASP.NET Core 1.0 RC2中使用RSS的正确方法

Mar*_*arn 14 rss asp.net-core-mvc asp.net-core asp.net-core-1.0

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

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

我知道在ASP.net 4.x中,你会使用RssReader或者SyndicationFeed我找不到ASP.net核心的等价物.

这是我得到的返回原始feed但我不知道如何从中提取数据.我想枚举这些项目,title然后description从每个项目中获取

    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();
    }
Run Code Online (Sandbox Code Playgroud)

Mar*_*arn 13

为了完整起见,我将在答案的"构建您自己的XML解析器"部分中包含最终代码,该代码是链接到示例@Sock的简化版本.@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; } 
}
Run Code Online (Sandbox Code Playgroud)

调节器

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Soc*_*ock 12

根据此问题,System.ServiceModel.Syndication尚未移植到ASP.NET Core.目前,这为您提供了两个选项:

  • 定位完整的.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"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

对此:

frameworks": {
  "net452": {
     "frameworkAssemblies": {
         "System.ServiceModel": ""
     }
   }
}
Run Code Online (Sandbox Code Playgroud)

构建自己的XML Parser

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