分析命名空间为的xml文件时出错
本文关键字:文件 出错 xml 命名空间 | 更新日期: 2023-09-27 18:01:06
xml位于此处:
https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml
我想要得到元素<Cube time="2016-08-04">
的time
属性。
我在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;
}
错误(我称之为Web服务(:
错误在这条线上
XmlNode node = nodes[0].SelectSingleNode("Cube");
错误:
由于内部错误,服务器无法处理该请求。有关该错误的详细信息,请在服务器上启用IncludeExceptionDetailInFaults(从ServiceBehaviorAttribute或从配置行为(以将异常信息发送回客户端,或者根据Microsoft.NET Framework SDK文档启用跟踪并检查服务器跟踪日志。
服务器堆栈跟踪:
在System.ServiceModel.Channels.ServiceChannel.SthrowIfFaultUnderstand(消息回复、消息故障、字符串操作、消息版本、FaultConverter故障转换器(
在System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime操作,ProxyRpc&rpc(
在System.ServiceModel.Channels.ServiceChannel.Call(字符串操作、布尔单向、ProxyOperationRuntime操作、Object[]输入、Object[]输出、TimeSpan超时(
在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作(
在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息(在[0]处重新引发异常:
在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage-reqMsg,IMessage-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;
尝试xml Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:'temp'test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants().Where(x => x.Name.LocalName == "Cube" && x.Attribute("currency") != null).Select(x => new {
currency = (string)x.Attribute("currency"),
rate = (double)x.Attribute("rate")
}).ToList();
}
}
}