I'm trying to select nodes from an rss feed. It works fine for Twitter, but I can't do it on Youtube.
我正在尝试从rss提要中选择节点。它在Twitter上运行得很好,但在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?
什么好主意吗?
7 个解决方案
#1
3
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'.
您正在尝试在一个空的名称空间中选择节点“entry”,而您应该尝试在名称空间“http://www.w3.org/2005/Atom”中选择节点“entry”。
You can use XMLNamespaceManager
to specify a default namespace:
可以使用XMLNamespaceManager指定一个默认名称空间:
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']"
或者你可以使用“/ *[本地名称()= '喂']/ *[本地名称()=“条目”)”
#2
1
Specify the namespace.
指定名称空间。
#3
1
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.
如果希望可视化XPath查询的结果,可以使用XpathVisualizer。这是一个WinForms的工具。加载要查询的XML文档,在查询中键入,查看结果。
alt text http://i45.tinypic.com/2jb409.jpg
alt文本http://i45.tinypic.com/2jb409.jpg
Free. Open source.
免费的。开源的。
#4
0
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.
如果它没有生成错误,那一定是因为xml文档不包含
#5
0
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.
但我的问题是,这个XPath查询是否正在检索任何内容。
#6
0
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);
#7
0
Give this a try: "/*[local-name()='feed']/*[local-name()='entry']"
给这一个尝试:“/ *(本地名称()= '喂']/ *[本地名称()=“条目”)”
#1
3
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'.
您正在尝试在一个空的名称空间中选择节点“entry”,而您应该尝试在名称空间“http://www.w3.org/2005/Atom”中选择节点“entry”。
You can use XMLNamespaceManager
to specify a default namespace:
可以使用XMLNamespaceManager指定一个默认名称空间:
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']"
或者你可以使用“/ *[本地名称()= '喂']/ *[本地名称()=“条目”)”
#2
1
Specify the namespace.
指定名称空间。
#3
1
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.
如果希望可视化XPath查询的结果,可以使用XpathVisualizer。这是一个WinForms的工具。加载要查询的XML文档,在查询中键入,查看结果。
alt text http://i45.tinypic.com/2jb409.jpg
alt文本http://i45.tinypic.com/2jb409.jpg
Free. Open source.
免费的。开源的。
#4
0
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.
如果它没有生成错误,那一定是因为xml文档不包含
#5
0
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.
但我的问题是,这个XPath查询是否正在检索任何内容。
#6
0
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);
#7
0
Give this a try: "/*[local-name()='feed']/*[local-name()='entry']"
给这一个尝试:“/ *(本地名称()= '喂']/ *[本地名称()=“条目”)”