SelectNode在StackOverflow Feed上不起作用
时间:2020-03-05 18:42:31 来源:igfitidea点击:
我试图在我的rss阅读器中添加对stackoverflow提要的支持,但是SelectNodes和SelectSingleNode无效。这可能与我还不了解的ATOM和xml名称空间有关。
我已经通过从feed标签中删除所有属性来使其正常工作,但这是一个hack,我想正确地做到这一点。那么,如何将SelectNode与原子供稿一起使用?
这是提要的一小段。
<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:thr="http://purl.org/syndication/thread/1.0"> <title type="html">StackOverflow.com - Questions tagged: c</title> <link rel="self" href="http://stackoverflow.com/feeds/tag/c" type="application/atom+xml" /> <subtitle>Check out the latest from StackOverflow.com</subtitle> <updated>2008-08-24T12:25:30Z</updated> <id>http://stackoverflow.com/feeds/tag/c</id> <creativeCommons:license>http://www.creativecommons.org/licenses/by-nc/2.5/rdf</creativeCommons:license> <entry> <id>http://stackoverflow.com/questions/22901/what-is-the-best-way-to-communicate-with-a-sql-server</id> <title type="html">What is the best way to communicate with a SQL server?</title> <category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="c" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="c++" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="sql" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="mysql" /><category scheme="http://stackoverflow.com/feeds/tag/c/tags" term="database" /> <author><name>Ed</name></author> <link rel="alternate" href="http://stackoverflow.com/questions/22901/what-is-the-best-way-to-communicate-with-a-sql-server" /> <published>2008-08-22T05:09:04Z</published> <updated>2008-08-23T04:52:39Z</updated> <summary type="html"><p>I am going to be using c/c++, and would like to know the best way to talk to a MySQL server. Should I use the library that comes with the server installation? Are they any good libraries I should consider other than the official one?</p></summary> <link rel="replies" type="application/atom+xml" href="http://stackoverflow.com/feeds/question/22901/answers" thr:count="2"/> <thr:total>2</thr:total> </entry> </feed>
解决方案
XmlDocument doc = new XmlDocument(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom"); doc.Load(feed); // successful XmlNodeList itemList = doc.DocumentElement.SelectNodes("atom:entry", nsmgr);
解决方案
回答
我们已经猜对了:我们正在请求不在名称空间中的节点,但是这些节点在名称空间中。
问题和解决方案的描述:http://weblogs.asp.net/wallen/archive/2003/04/02/4725.aspx
回答
我们可能需要添加XmlNamespaceManager。
XmlDocument document = new XmlDocument(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable); nsmgr.AddNamespace("creativeCommons", "http://backend.userland.com/creativeCommonsRssModule"); // AddNamespace for other namespaces too. document.Load(feed);
如果要在使用SelectNode的文档上调用它们,则需要它。我们看到什么错误?
回答
我只想用..
XmlNodeList itemList = xmlDoc.DocumentElement.SelectNodes("entry");
但是,入口标签属于哪个名称空间?我会假设xmlns =" http://www.w3.org/2005/Atom",但它没有标题,那么我该如何添加该名称空间?
XmlDocument document = new XmlDocument(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable); nsmgr.AddNamespace("", "http://www.w3.org/2005/Atom"); document.Load(feed);
这样的东西吗?
回答
不要将XML文件中的名称空间名称与名称空间管理器的名称空间名称混淆。它们都是捷径,不一定要匹配。
因此,我们可以将" http://www.w3.org/2005/Atom"注册为" atom",然后为" atom:entry"执行一个SelectNodes。