Question

J'essaie d'ajouter la prise en charge des flux stackoverflow dans mon lecteur RSS mais Sélectionner des nœuds et Sélectionner un nœud unique n'ont aucun effet.C'est probablement quelque chose à voir avec les espaces de noms ATOM et XML que je ne comprends pas encore.

Je l'ai fait fonctionner en supprimant tous les attributs du alimentation tag, mais c'est un hack et j'aimerais le faire correctement.Alors, comment utiliser Sélectionner des nœuds avec des alimentations atomiques ?

Voici un extrait du flux.

<?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 solution

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);
Était-ce utile?

La solution

Ne confondez pas les noms d'espace de noms dans le fichier XML avec les noms d'espace de noms de votre gestionnaire d'espace de noms.Ce sont tous deux des raccourcis et ils ne doivent pas nécessairement correspondre.

Vous pouvez donc vous inscrire "http://www.w3.org/2005/Atom" comme "atome", puis effectuez un SelectNodes pour "atom:entry".

Autres conseils

Vous devrez peut-être ajouter 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);

Il est nécessaire si vous souhaitez appeler SelectNodes sur un document qui les utilise.Quelle erreur voyez-vous ?

Vous avez bien deviné :vous demandez des nœuds qui ne se trouvent pas dans un espace de noms, mais ces nœuds sont dans un espace de noms.

Description du problème et solution : http://weblogs.asp.net/wallen/archive/2003/04/02/4725.aspx

Je veux juste utiliser ..

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

mais quel espace de noms fait le entrée les balises relèvent-elles ?Je suppose que xmlns="http://www.w3.org/2005/Atom", mais il n'a pas de titre, alors comment ajouter cet espace de noms ?

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

Quelque chose comme ca?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top