Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 没有使用XPath从Atom XML文档中选择节点?_C#_Xml_Xpath_Xmldocument_Atom Feed - Fatal编程技术网

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

C# 没有使用XPath从Atom XML文档中选择节点?,c#,xml,xpath,xmldocument,atom-feed,C#,Xml,Xpath,Xmldocument,Atom Feed,我试图以编程方式解析Atom提要。我将atom XML作为字符串下载。我可以将XML加载到XmlDocument中。但是,我无法使用XPath遍历文档。无论何时我尝试,都会得到空值 我一直在使用这个Atom提要作为测试: 调用SelectSingleNode总是返回null,除非我使用/。以下是我现在正在尝试的: using (WebClient wc = new WebClient()) { string xml = wc.DownloadString("http://steve-ye

我试图以编程方式解析Atom提要。我将atom XML作为字符串下载。我可以将XML加载到XmlDocument中。但是,我无法使用XPath遍历文档。无论何时我尝试,都会得到空值

我一直在使用这个Atom提要作为测试:

调用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提要上工作,我需要做什么

编辑 虽然这个问题有答案,但我发现这个问题有一个几乎完全相同的答案:


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

从字符串加载XML并查找任何“错误/错误”节点

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlResult);            
XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);
nm.AddNamespace("ns", "http://somedomain.com/namespace1/2"); //ns - any name, make sure it is same in the below line

XmlNodeList errors = xmlDoc.SelectNodes("/ns:*//ns:Errors/ns:Error", nm);       
-Mathulan

可能重复的