Domanda

Sto cercando di aggiungere il supporto per i feed StackOverflow nel mio lettore RSS ma Seleziona Nodi E Selezionare Nodo singolo non hanno alcun effetto.Probabilmente ha qualcosa a che fare con gli spazi dei nomi ATOM e xml che ancora non capisco.

L'ho fatto funzionare rimuovendo tutti gli attributi da foraggio tag, ma è un trucco e vorrei farlo correttamente.Quindi, come si usa Seleziona Nodi con feed atomici?

Ecco uno snippet del 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">&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="http://stackoverflow.com/feeds/question/22901/answers" thr:count="2"/>
   <thr:total>2</thr:total>
 </entry>


</feed>


La soluzione

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);
È stato utile?

Soluzione

Non confondere i nomi degli spazi dei nomi nel file XML con i nomi degli spazi dei nomi del gestore dello spazio dei nomi.Sono entrambe scorciatoie e non devono necessariamente corrispondere.

Quindi puoi registrarti "http://www.w3.org/2005/Atom" come "atom", quindi eseguire un SelectNodes per "atom:entry".

Altri suggerimenti

Potrebbe essere necessario aggiungere un 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);

È necessario se vuoi chiamare SelectNodes su un documento che li utilizza.Quale errore vedi?

Hai indovinato correttamente:stai chiedendo nodi non in uno spazio dei nomi, ma questi nodi si trovano in uno spazio dei nomi.

Descrizione del problema e soluzione: http://weblogs.asp.net/wallen/archive/2003/04/02/4725.aspx

voglio solo usare..

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

ma quale spazio dei nomi fa il iscrizione i tag rientrano?Presumo xmlns="http://www.w3.org/2005/Atom", ma non ha titolo, quindi come aggiungerei quello spazio dei nomi?

XmlDocument document = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
nsmgr.AddNamespace("", "http://www.w3.org/2005/Atom");
document.Load(feed);

Qualcosa del genere?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top