【问题标题】:Error parsing xml file with namespace使用命名空间解析 xml 文件时出错
【发布时间】:2016-12-11 06:13:54
【问题描述】:

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;
    }

错误(我从网络服务调用它):

错误就在这一行

XmlNode node = nodes[0].SelectSingleNode("Cube");

错误:

由于内部错误,服务器无法处理请求。有关该错误的更多信息,请在服务器上打开 IncludeExceptionDetailInFaults(来自 ServiceBehaviorAttribute 或来自配置行为)以便将异常信息发送回客户端,或者根据 Microsoft .NET Framework SDK 文档打开跟踪并检查服务器跟踪日志。

服务器堆栈跟踪:
在 System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(消息回复、MessageFault 故障、字符串操作、MessageVersion 版本、FaultConverter 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()

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    您正在忽略 XML 中的 default 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;
    

    【讨论】:

      【解决方案2】:

      试试 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();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-04-07
        • 2021-05-03
        • 2014-01-10
        • 2020-09-11
        • 2010-11-08
        • 2018-07-15
        • 2012-06-11
        相关资源
        最近更新 更多