views:

42

answers:

3

Hello,

this is my first post here - usually i'll find the answer somewhere in this board - but not today :-(

okay: I,m trying to read a XML-RSS-Feed from a website. Therefore i use a async download and create a XDocument with the XDocument.Parse() Method.

The Document intends to be very simple, like this:

<root>
  <someAttribute></SomeAttribute>
  <item>...</item>
  <item>...</item>
</root>

Now i want to read out all the items. Therefore i tried:

foreach (XElement NewsEntry in xDocument.Descendants("item"))

but this doesn't work. So i found a post in this board to use the qualified name, because there are some namespaces defined in the root element:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns="http://purl.org/rss/1.0/"&gt;

well, i tried all 3 available namespaces - nothing worked for me:

XName itemName = XName.Get("item", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
XName itemName2 = XName.Get("item", "http://purl.org/dc/elements/1.1/");
XName itemName3 = XName.Get("item", "http://purl.org/rss/1.0/modules/syndication/");

Any help would be pretty cool. (Usually i'm doing the XML-Analysis with Regex - but this time i'm developing for a mobile device, and so should take care of the performance.)

A: 

You have not tried the default namespace at the end of the rdf declaration:

xmlns="http://purl.org/rss/1.0/"

This makes sense, as any element in the default namespace will not need to have the namespace prepended to the element name.

Oded
Yeah, that's it - the one solution i didn't expect. Thx very much.
dognose
@dognose - No problem. Would appreciate an upvote and you accepting the answer ;)
Oded
A: 

Not directly a solution to the XDocument RSS read problem. But why aren't you using the provided SyncdicationFeed class to load the feed? http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx

RonaldV
looks very interesting. i'll have a look at that, too.
dognose
A: 

Try this

var elements = from p in xDocument.Root.Elements()
where p.Name.LocalName == "item"
select p;

foreach(var element in elements)
{
//Do stuff
}
rohk