Error Reading RSS Feed using LINQ to XML

烈酒焚心 提交于 2019-12-02 07:47:56

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

        }

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!