从 feed 中提取 XML 信息?

发布于 2024-09-10 22:55:37 字数 477 浏览 0 评论 0原文

我尝试从来自 youtube 的 xml feed 中提取特定数据。

XML 链接:http://gdata.youtube.com/feeds/api /videos/WFPnl8aEPgo?alt=rss

我已经能够提取如下信息:

标题, 描述

使用此查询字符串的

将标题变暗为字符串= videoInfoNavigator.SelectSingleNode("/item[1]/title").Value

但是,我无法找到正确的查询字符串来获取

media:keywords等信息

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

XML link: http://gdata.youtube.com/feeds/api/videos/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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一江春梦 2024-09-17 22:55:38

media:keywords 使用 media 命名空间前缀,该前缀绑定到 namespace-uri http ://search.yahoo.com/mrss/

如果您可以注册命名空间前缀,则可以使用如下所示的 XPATH:

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

但是,如果您需要不依赖于命名空间前缀的更通用的 XPATH,则您可以可以这样表达:

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

应用于您的示例代码:

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

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
强者自强 2024-09-17 22:55:38

试试这个:

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);

请注意,您使用的前缀根本不重要。它只是命名空间的缩写。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文