且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误读取使用LINQ to XML RSS订阅

更新时间:1970-01-01 07:55:12

您有标签两层。第一信道,然后将物品。请参见下面code。

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

        }

    }
}