C# SelectNodes 不适用于 stackoverflow 提要

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:13:52  来源:igfitidea点击:

SelectNodes not working on stackoverflow feed

提问by sieben

I'm trying to add support for stackoverflow feeds in my rss reader but SelectNodesand SelectSingleNodehave no effect. This is probably something to do with ATOM and xml namespaces that I just don't understand yet.

我正在尝试在我的 rss 阅读器中添加对 stackoverflow 提要的支持,但SelectNodesSelectSingleNode没有效果。这可能与我还不明白的 ATOM 和 xml 命名空间有关。

I have gotten it to work by removing all attributes from the feedtag, but that's a hack and I would like to do it properly. So, how do you use SelectNodeswith atom feeds?

我已经通过从提要标签中删除所有属性来让它工作,但这是一个黑客,我想正确地做到这一点。那么,如何将SelectNodes与 atom 提要一起使用?

Here's a snippet of the 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>



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

采纳答案by Brad Wilson

Don't confuse the namespace names in the XML file with the namespace names for your namespace manager. They're both shortcuts, and they don't necessarily have to match.

不要将 XML 文件中的命名空间名称与命名空间管理器的命名空间名称混淆。它们都是捷径,不一定要匹配。

So you can register "http://www.w3.org/2005/Atom" as "atom", and then do a SelectNodes for "atom:entry".

所以你可以将“ http://www.w3.org/2005/Atom”注册为“atom”,然后为“atom:entry”做一个SelectNodes。

回答by Brad Wilson

You've guessed correctly: you're asking for nodes not in a namespace, but these nodes are in a namespace.

您猜对了:您要求的节点不在名称空间中,但这些节点在名称空间中。

Description of the problem and solution: http://weblogs.asp.net/wallen/archive/2003/04/02/4725.aspx

问题描述及解决方法:http: //weblogs.asp.net/wallen/archive/2003/04/02/4725.aspx

回答by Julio César

You might need to add a XmlNamespaceManager.

您可能需要添加一个 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);

It is needed if you want to call SelectNodes on a document that uses them. What error are you seeing?

如果您想在使用它们的文档上调用 SelectNodes,则需要它。你看到什么错误?

回答by sieben

I just want to use..

我只想用..

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

but, what namespace do the entrytags fall under? I would assume xmlns="http://www.w3.org/2005/Atom", but it has no title so how would I add that namespace?

但是,条目标签属于哪个命名空间?我假设 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);

Something like that?

类似的东西?