Попытка остаться рядом с тем, что у вас есть, перечислит все URL-адреса из среды = "image" в канале CNN rss:
var ns = (XNamespace) "http://search.yahoo.com/mrss/"; // namespace of the extension
SyndicationFeed feed = SyndicationFeed.Load(XmlReader.Create(@"http://rss.cnn.com/rss/edition.rss"));
var urls = from item in feed.Items // all items
from ext in item.ElementExtensions // all extensions to ext
where ext.OuterName == "group" && // find the ones called group
ext.OuterNamespace == ns // in the right namespace
from content in ext.GetObject<XElement>().Elements(ns + "content") // get content elements
where (string) content.Attribute("medium") == "image" // if that medium is an image
select (string) content.Attribute("url"); // get the url
// output what is in urls
foreach(string url in urls)
{
Console.WriteLine(url);
}
Самое важное то, что GetObject<XElement>()
дал вам *Элемент 1005 *, но этот элемент сам по себе не имеет атрибутов medium
и url
.Вы должны перечислить дочерние элементы с именем content
, и у каждого из этих элементов есть интересующие вас атрибуты.
Если вам нужны ширина и высота, вы можете легко спроецировать их в окончательном выборе.:
select new {
url = (string) content.Attribute("url"),
h = (int) content.Attribute("height"),
w = (int) content.Attribute("width")
};