tags:

views:

92

answers:

7

I'm trying to select nodes from an rss feed. It works fine for Twitter, but I can't do it on Youtube.

        string youtube = "http://gdata.youtube.com/feeds/api/users/CTVOlympics/uploads";
        string twitter = "http://twitter.com/statuses/user_timeline/ctvolympics.rss";

        //this populates the XmlNodeList object
        XmlTextReader readerTwitter = new XmlTextReader(twitter);
        XmlDocument docTwitter = new XmlDocument();
        docTwitter.Load(readerTwitter);
        XmlNodeList nodesTwitter = docTwitter.SelectNodes("/rss/channel/item");

        //this doesn't populate the object
        XmlTextReader readerYoutube = new XmlTextReader(youtube);
        XmlDocument docYoutube = new XmlDocument();
        docYoutube.Load(readerYoutube);
        XmlNodeList nodesYoutube = docYoutube.SelectNodes("/feed/entry");

any ideas?

+2  A: 

Specify the namespace.

Doug D
See XmlNode..::.SelectNodes Method (String, XmlNamespaceManager) http://msdn.microsoft.com/en-us/library/4bektfx9.aspx
Doug D
A: 

If it does not generate an error, it must be because the xml-document doesn't contain a <feed>-element, or <entry> elements with a <feed>-parent.

Frederik Wordenskjold
I was really hoping that everyone who cared enough to help would actually look at the xml I left so silly "answers" like this wouldn't be given.
@codemonkey12: this is a correct answer. It does not have a (namespace-qualified) `feed` element.
John Saunders
A: 

Try getting at the base of the problem by using the // instead of /. So something like //entry my work better as it will plumb the depths looking for your request.

But my question is whether that XPath query is actually retrieving anything.

Chuck Vose
+4  A: 

You're attempting to select the node 'entry' in an empty namespace, whereas you should be trying to select the node 'entry' in the namespace 'http://www.w3.org/2005/Atom'.

You can use XMLNamespaceManager to specify a default namespace:

XmlNamespaceManager nsmanager = new XmlNamespaceManager(docYoutube.NameTable);
nsmanager.AddNamespace(String.Empty, "http://www.w3.org/2005/Atom");

or you could use "/*[local-name()='feed']/*[local-name()='entry']"

Sam
+2  A: 

If you want to visualize the result of XPath queries, you can use the XpathVisualizer. It's a WinForms tool. Load the XML document you want to query, key in the query, view the results.

alt text

Free. Open source.

Cheeso
+1 thanks for the great tool reference
Awesome tool, thank you!!!
Marcel Gheorghita
A: 
string youtube = "http://gdata.youtube.com/feeds/api/users/CTVOlympics/uploads";
string twitter = "http://twitter.com/statuses/user_timeline/ctvolympics.rss";

//this populates the XmlNodeList object
XmlDocument docTwitter;
using (var readerTwitter = XmlReader.Create(twitter))
{
    docTwitter = new XmlDocument();
    docTwitter.Load(readerTwitter);
}
XmlNodeList nodesTwitter = docTwitter.SelectNodes("/rss/channel/item");

//this doesn't populate the object
XmlDocument docYoutube;
using (var readerYoutube = XmlReader.Create(youtube))
{
    docYoutube = new XmlDocument();
    docYoutube.Load(readerYoutube);
}
XmlNamespaceManager ns = new XmlNamespaceManager(docYoutube.NameTable);
ns.AddNamespace("atom", "http://www.w3.org/2005/Atom");
XmlNodeList nodesYoutube = docYoutube.SelectNodes("/atom:feed/atom:entry", ns);
John Saunders
A: 

if you are using .net 3.5 you could use linq to xml, is much easier.

Pablo Castilla
example, please.
John Saunders