I am trying to parse RSS feed from BBC, but it return nothing ! RSS Feed
http://www.bbc.co.uk/arabic/middleeast/index.xml
My code
var item = (from descendant in document.Descendants("entry")
select new NewsItem()
{
link = descendant.Element("link").Attribute("href").Value,
description = descendant.Element("summary").Value,
title = descendant.Element("title").Value,
image = " " // entry > link > img media:content > second media:thumbnail > url attribute
entry_date = DateTime.Now,
category = " " // second descendant.Elements("category") > label
}).ToList();
You're looking for elements in no namespace. From the root element of the RSS feed:
<feed xmlns="http://www.w3.org/2005/Atom"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/">
The xmlns="..."
attribute specifies the default namespace for descendant elements (and that one).
So you want:
XNamespace ns = "http://www.w3.org/2005/Atom";
var item = document.Descendants(ns + "entry")
.Select(entry => new NewsItem
{
link = entry.Element(ns + "link")
.Attribute("href").Value,
description = entry.Element(ns + "summary").Value,
title = entry.Element(ns + "title").Value,
image = " "
entry_date = DateTime.Now,
category = " "
})
.ToList();
Note how I've removed the query expression here, just using method calls instead - if a query is just "from x select y" then the query expression just adds cruft.
Additionally, I'd strongly recommend that you start following .NET naming conventions (e.g. EntryDate
instead of entry_date
- although the value for that example is incorrect too...).
EDIT: As noted in comments, you could also use SyndicationFeed
or a third-party library to parse the feed. You're not the first person to want to parse RSS in .NET :)
See more on this question at Stackoverflow