16

RSSリーダーに stackoverflow フィードのサポートを追加しようとしていますが、 SelectNodesSelectSingleNodeは効果がありません。これはおそらく、私がまだ理解していない ATOM および xml 名前空間に関係している可能性があります。

フィードタグからすべての属性を削除することで機能するようになりましたが、それはハックであり、適切に行いたいと考えています。では、AtomフィードでSelectNodesをどのように使用するのでしょうか?

フィードのスニペットを次に示します。

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


ソリューション

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);
4

4 に答える 4

9

XML ファイル内の名前空間名と名前空間マネージャーの名前空間名を混同しないでください。どちらもショートカットであり、必ずしも一致する必要はありません。

したがって、「http://www.w3.org/2005/Atom」を「atom」として登録し、「atom:entry」に対して SelectNodes を実行できます。

于 2008-08-24T01:03:03.587 に答える
6

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 を使用するドキュメントで SelectNodes を呼び出したい場合に必要です。どのようなエラーが表示されますか?

于 2008-08-24T00:45:22.693 に答える
2

ご想像のとおり、名前空間にないノードを要求していますが、これらのノードは名前空間にあります。

問題と解決策の説明: http://weblogs.asp.net/wallen/archive/2003/04/02/4725.aspx

于 2008-08-24T00:43:43.227 に答える
0

使いたいだけ..

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);

そんな感じ?

于 2008-08-24T01:00:40.130 に答える