我正在尝试从以下xml中提取信息:ezb
XmlDocument doc = new XmlDocument();
doc.Load("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
var x = doc.SelectSingleNode("//@time");
labelDate.Text= "Data From: " + x.Value;
工作得很好! 什么不起作用,但据我所知,xpath语法是正确的:
var z = doc.SelectSingleNode("//Cube[@currency='JPY']/@rate");
labelResult.Text = z.Value;
我得到一个例外:“对象引用未设置为对象的实例”。 据我所知,我没有从我的查询中获取任何奇怪的数据,因为我很确定它是正确的。我感谢任何帮助!
答案 0 :(得分:0)
您错过了xml文件具有命名空间的事实。以下是如何将它们包含在您的查询中:
XmlDocument doc = new XmlDocument();
doc.Load("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(doc.NameTable);
xmlnsManager.AddNamespace("gesmes", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
var z = doc.SelectSingleNode("//gesmes:Cube[@currency='JPY']/@rate", xmlnsManager);
您的第一个查询有效,因为名称空间不适用于属性。