gpt4 book ai didi

c# - 没有使用 XPath 从 Atom XML 文档中选择节点?

转载 作者:数据小太阳 更新时间:2023-10-29 01:50:22 25 4
gpt4 key购买 nike

我正在尝试以编程方式解析 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 提要上工作?

编辑

Although this question has an answer, I found out this question has an almost exact duplicate: SelectNodes not working on stackoverflow feed

最佳答案

虽然 C# 实现可能允许默认命名空间(我不知道),但 XPath 1.0 规范不允许。所以,给“Atom”一个前缀:

nsMngr.AddNamespace("atom", "http://www.w3.org/2005/Atom");

并适本地更改您的 XPath:

XmlNode node = atom.SelectSingleNode("//atom:entry/atom:link/app:edited", nsMngr);

关于c# - 没有使用 XPath 从 Atom XML 文档中选择节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/501171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com