我正在尝试以编程方式解析 Atom 提要。我将原子 XML 作为字符串下载。我可以将 XML 加载到XmlDocument
. 但是,我无法使用 XPath 遍历文档。每当我尝试时,我都会得到null
.
我一直在使用这个 Atom 提要作为测试:http ://steve-yegge.blogspot.com/feeds/posts/default
调用SelectSingleNode()
总是返回null
,除非我使用“ /
”。这是我现在正在尝试的:
using (WebClient wc = new WebClient())
{
string xml = wc.DownloadString("http://steve-yegge.blogspot.com/feeds/posts/default");
XmlNamespaceManager nsMngr = new XmlNamespaceManager(new NameTable());
nsMngr.AddNamespace(string.Empty, "http://www.w3.org/2005/Atom");
nsMngr.AddNamespace("app", "http://purl.org/atom/app#");
XmlDocument atom = new XmlDocument();
atom.LoadXml(xml);
XmlNode node = atom.SelectSingleNode("//entry/link/app:edited", nsMngr);
}
我认为这可能是因为我的 XPath,所以我也尝试了对根节点的简单查询,因为我知道根应该可以工作:
// I've tried both with & without the nsMngr declared above
XmlNode node = atom.SelectSingleNode("/feed");
无论我做什么,它似乎都无法选择任何东西。显然我错过了一些东西,我只是不知道是什么。为了使 XPath 在这个 Atom 提要上工作,我需要做什么?
编辑
虽然这个问题有答案,但我发现这个问题几乎完全相同:SelectNodes not working on stackoverflow feed