【问题标题】:XML to JSON or To where to use it in .NET c#XML 到 JSON 或在 .NET c# 中使用它的位置
【发布时间】:2010-09-18 10:20:29
【问题描述】:

如何从这个 xml 将数据收集到 c# 中? 我从:http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

获取数据

PS:我想从网络服务中获取货币汇率,但我找不到值得信赖的网络服务,这就是我尝试使用 xml 的原因...

  <?xml version="1.0" encoding="UTF-8" ?> 
- <gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
  <gesmes:subject>Reference rates</gesmes:subject> 
- <gesmes:Sender>
  <gesmes:name>European Central Bank</gesmes:name> 
  </gesmes:Sender>
- <Cube>
- <Cube time="2010-09-17">
  <Cube currency="USD" rate="1.3060" /> 
  <Cube currency="JPY" rate="111.98" /> 
  <Cube currency="BGN" rate="1.9558" /> 
  <Cube currency="CZK" rate="24.680" /> 
  <Cube currency="DKK" rate="7.4468" /> 
  <Cube currency="EEK" rate="15.6466" /> 
  <Cube currency="GBP" rate="0.83575" /> 
  <Cube currency="HUF" rate="282.82" /> 
  <Cube currency="LTL" rate="3.4528" /> 
  <Cube currency="LVL" rate="0.7087" /> 
  <Cube currency="PLN" rate="3.9622" /> 
  <Cube currency="RON" rate="4.2580" /> 
  <Cube currency="SEK" rate="9.2295" /> 
  <Cube currency="CHF" rate="1.3210" /> 
  <Cube currency="NOK" rate="7.9650" /> 
  <Cube currency="HRK" rate="7.2845" /> 
  <Cube currency="RUB" rate="40.4850" /> 
  <Cube currency="TRY" rate="1.9606" /> 
  <Cube currency="AUD" rate="1.3886" /> 
  <Cube currency="BRL" rate="2.2419" /> 
  <Cube currency="CAD" rate="1.3410" /> 
  <Cube currency="CNY" rate="8.7809" /> 
  <Cube currency="HKD" rate="10.1425" /> 
  <Cube currency="IDR" rate="11713.52" /> 
  <Cube currency="INR" rate="59.8530" /> 
  <Cube currency="KRW" rate="1515.90" /> 
  <Cube currency="MXN" rate="16.7075" /> 
  <Cube currency="MYR" rate="4.0512" /> 
  <Cube currency="NZD" rate="1.7940" /> 
  <Cube currency="PHP" rate="57.700" /> 
  <Cube currency="SGD" rate="1.7442" /> 
  <Cube currency="THB" rate="40.153" /> 
  <Cube currency="ZAR" rate="9.3307" /> 
  </Cube>
  </Cube>
  </gesmes:Envelope>

【问题讨论】:

  • “检索”是什么意思?
  • 我不明白这个问题与 JSON 有什么关系。

标签: c# .net asp.net xml json


【解决方案1】:

第一步看起来像:

var doc = XDocument.Load(source);
var ns = doc.Root.GetDefaultNamespace();

var topCube = doc.Root.Element(ns+"Cube");
var timeCube = topCube.Element(ns+"Cube");

string time = timeCube.Attribute("time").Value;

var cubes = from c in timeCube.Elements()
            select new 
            {  Key = c.Attribute("currency").Value, 
               Value = decimal.Parse(c.Attribute("rate").Value, CultureInfo.InvariantCulture) 
            };

也许可以这样转换:

Dictionary<string,decimal> lookup = cubes.ToDictionary(a => a.Key, a => a.Value);
decimal dollar = lookup["USD"];

【讨论】:

    猜你喜欢
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    相关资源
    最近更新 更多