Подтвердить что ты не робот

SelectNodes не работает с лентой stackoverflow

Я пытаюсь добавить поддержку лент stackoverflow в моем rss-ридере, но SelectNodes и SelectSingleNode не действуют. Вероятно, это связано с пространствами имен ATOM и xml, которые я пока еще не понимаю.

Я получил его для работы, удалив все атрибуты из тега feed, но это взломать, и я бы хотел сделать это правильно. Итак, как вы используете SelectNodes с каналами Atom?

Вот фрагмент фида.

<?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="#" onclick="location.href='http://stackoverflow.com/feeds/tag/c'; return false;" 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/info/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="#" onclick="location.href='http://stackoverflow.com/info/22901/what-is-the-best-way-to-communicate-with-a-sql-server'; return false;" />
   <published>2008-08-22T05:09:04Z</published>
   <updated>2008-08-23T04:52:39Z</updated>
   <summary type="html">&lt;p&gt;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?&lt;/p&gt;</summary>
   <link rel="replies" type="application/atom+xml" href="#" onclick="location.href='http://stackoverflow.com/feeds/question/22901/answers'; return false;" 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);
4b9b3361

Ответ 1

Не путайте имена пространства имен в файле XML с именами пространства имен для вашего диспетчера пространства имен. Они оба являются ярлыками, и они не обязательно должны совпадать.

Итак, вы можете зарегистрировать " http://www.w3.org/2005/Atom" как "атом", а затем сделать SelectNodes для "atom: entry".

Ответ 2

Вам может потребоваться добавить 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);

Это необходимо, если вы хотите вызвать SelectNodes в документе, который их использует. Какую ошибку вы видите?

Ответ 3

Вы правильно догадались: вы запрашиваете узлы не в пространстве имен, но эти узлы находятся в пространстве имен.

Описание проблемы и решения: http://weblogs.asp.net/wallen/archive/2003/04/02/4725.aspx

Ответ 4

Я просто хочу использовать..

XmlNodeList itemList = xmlDoc.DocumentElement.SelectNodes("entry");

но какое пространство имен содержит теги 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);

Что-то вроде этого?