SelectNodes não funciona no feed stackoverflow
Pergunta
Estou tentando adicionar suporte para feeds stackoverflow em meu leitor rss, mas SelecioneNodes e SelecioneSingleNode não têm efeito.Provavelmente isso tem algo a ver com namespaces ATOM e xml que ainda não entendi.
Eu fiz funcionar removendo todos os atributos do alimentar tag, mas isso é um hack e eu gostaria de fazê-lo corretamente.Então, como você usa SelecioneNodes com feeds de átomos?
Aqui está um trecho do feed.
<?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>
A solução
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);
Solução
Não confunda os nomes de namespace no arquivo XML com os nomes de namespace do seu gerenciador de namespace.Ambos são atalhos e não precisam necessariamente ser iguais.
Então você pode se registrar "http://www.w3.org/2005/Atom" como "atom" e, em seguida, faça um SelectNodes para "atom:entry".
Outras dicas
Talvez seja necessário adicionar um 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);
É necessário se você quiser chamar SelectNodes em um documento que os utiliza.Que erro você está vendo?
Você adivinhou corretamente:você está solicitando nós que não estão em um namespace, mas esses nós estão em um namespace.
Descrição do problema e solução: http://weblogs.asp.net/wallen/archive/2003/04/02/4725.aspx
só quero usar..
XmlNodeList itemList = xmlDoc.DocumentElement.SelectNodes("entry");
mas, qual namespace o entrada tags se enquadram?Eu assumiria xmlns="http://www.w3.org/2005/Atom", mas não tem título, então como adicionaria esse namespace?
XmlDocument document = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("", "http://www.w3.org/2005/Atom");
document.Load(feed);
Algo parecido?