xml位于此处:
https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
我想获取time
元素的属性<Cube time="2016-08-04">
。
我在C#中的代码如下,但出现错误。
private string function()
{
XmlDocument Doc = new XmlDocument();
Doc.Load("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(Doc.NameTable);
nsmgr.AddNamespace("gesmes", "http://www.gesmes.org/xml/2002-08-01");
XmlNodeList nodes = Doc.SelectNodes("gesmes:Envelope/Cube", nsmgr);
XmlNode node = nodes[0].SelectSingleNode("Cube");
return node.Attributes["time"].Value;
}
错误(我从网络服务调用):
错误在这条线上
XmlNode node = nodes[0].SelectSingleNode("Cube");
错误:
由于内部错误,服务器无法处理请求。有关错误的更多信息,请打开服务器上的IncludeExceptionDetailInFaults(从ServiceBehaviorAttribute或从配置行为),以便将异常信息发送回客户端,或者按照Microsoft .NET Framework SDK文档和检查服务器跟踪日志。
服务器堆栈跟踪:
位于System.ServiceModel.Channels.ServiceChannel.HandleReply(
ProxyOperationRuntime操作,ProxyRpc&rpc)
处,位于System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(消息答复,MessageFault错误,字符串操作,MessageVersion版本,FaultConverter faultConverter)。 ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔型单向,ProxyOperationRuntime操作,系统处的Object [] ins,Object [] outs,TimeSpan超时),
位于System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)
。 ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息)在异常重新抛出[0]:
在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(即时聊天reqMsg,即时聊天retMsg)
在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&MSGDATA,的Int32类型)
在IRate.DoWork( )在RateClient.DoWork()
你忽略了默认的XML XML命名空间!
<gesmes:Envelope
xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01"
xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
***** Default XML namespace
试试下面的代码:
XmlDocument Doc = new XmlDocument();
Doc.Load("https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(Doc.NameTable);
nsmgr.AddNamespace("gesmes", "http://www.gesmes.org/xml/2002-08-01");
// add another namespace alias for the *default* namespace
nsmgr.AddNamespace("default", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
// *USE* the default namespace for those nodes that don't have an explicit
// XML namespace alias in your XML document
XmlNodeList nodes = Doc.SelectNodes("gesmes:Envelope/default:Cube", nsmgr);
XmlNode node = nodes[0].SelectSingleNode("default:Cube", nsmgr);
string value = node.Attributes["time"].Value;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句