开发者

Extract XML info from feed?

开发者 https://www.devze.com 2023-01-07 12:58 出处:网络
I try to extract specific data from an xml feed that comes from youtube. XML link: http://gdata.youtube.com/feeds/api/vid开发者_StackOverfloweos/WFPnl8aEPgo?alt=rss

I try to extract specific data from an xml feed that comes from youtube.

XML link: http://gdata.youtube.com/feeds/api/vid开发者_StackOverfloweos/WFPnl8aEPgo?alt=rss

I've been able to extract info like:

Title, Description

using this query string:

Dim Title As String = videoInfoNavigator.SelectSingleNode("/item[1]/title").Value

However, I'm not able to find the proper query string to get info like

media:keywords


The media:keywords use the media namespace prefix, which is bound to the namespace-uri http://search.yahoo.com/mrss/

If you can register the namespace prefix, you could use an XPATH like this:

/item[1]/media:group/media:keywords

However, if you need a more generic XPATH that does not rely on the namespace prefix, you could express it like this:

/item[1]/*[local-name()='group' and namespace-uri()='http://search.yahoo.com/mrss/']/*[local-name()='keywords' and namespace-uri()='http://search.yahoo.com/mrss/']

Applied to your example code:

Dim Keywords As String = videoInfoNavigator.SelectSingleNode("/item[1]/*[local-name()='group' and namespace-uri()='http://search.yahoo.com/mrss/']/*[local-name()='keywords' and namespace-uri()='http://search.yahoo.com/mrss/']").Value


Try this:

XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("m", "http://search.yahoo.com/mrss/");
var keywords = doc.CreateNavigator().SelectSingleNode("/item/m:group/m:keywords", ns);
Console.WriteLine(keywords.Value);

Note that the prefix you use doesn't matter at all. It's just an abbreviation for the namespace.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号