XPath does not return results - YouTube channel

I am trying to use XPath in C # to extract the href value in <link> nodes into the most popular Atom Atom feed .

From the documentation I read on the Internet, this process would be relatively simple, something like:

XmlDocument xml = new XmlDocument();
xml.Load("http://gdata.youtube.com/feeds/api/standardfeeds/most_popular");
XmlNodeList linkNodes;
linkNodes = xml.SelectNodes("/feed/entry/link[@rel='alternate']");

But this does not work, I do not get any results. I tried adding namespaces using the XmlNamespaceManager, but that doesn't help either.

hasty response will be appreciated! Thank!

+3
source share
2 answers

I am sure that adding namespaces correctly would help, since I am sure the problem is. Personally, I would use LINQ for XML, though. Code example:

using System;
using System.Linq;
using System.Xml.Linq;

public class Test
{
    static void Main()
    {
        string url =
             "http://gdata.youtube.com/feeds/api/standardfeeds/most_popular";
        var doc = XDocument.Load(url);
        XNamespace ns = "http://www.w3.org/2005/Atom";
        var links = doc.Root
                       .Elements(ns + "entry")
                       .Elements(ns + "link")
                       .Where(x => (string) x.Attribute("rel") == "alternate");

        Console.WriteLine(links.Count()); // 25
    }
}
+4
source

, , ( ;-), , , :

, . , XmlDocument , ; "" ; xmlns=http://www.w3.org/2005/Atom

    XmlDocument xdoc = new XmlDocument();
    xdoc.Load("http://gdata.youtube.com/feeds/api/standardfeeds/most_popular");

    XmlNamespaceManager manager = new XmlNamespaceManager(xdoc.NameTable);
    manager.AddNamespace("base", "http://www.w3.org/2005/Atom");

    var nodes = xdoc.SelectNodes("/base:feed/base:entry/base:link[@rel='alternate']", manager);

XmlNodeList, 25 link.

+3

All Articles