C# XML到JSON或在.NETC中使用它的位置#

C# XML到JSON或在.NETC中使用它的位置#,c#,.net,asp.net,xml,json,C#,.net,Asp.net,Xml,Json,如何从这个xml将数据收集到c#中? 我从以下方面获得数据: PS:我想从web服务获取货币汇率,但我找不到可靠的web服务,这就是为什么我尝试使用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/e

如何从这个xml将数据收集到c#中? 我从以下方面获得数据:

PS:我想从web服务获取货币汇率,但我找不到可靠的web服务,这就是为什么我尝试使用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>

- 
参考利率
- 
欧洲中央银行
- 
- 

第一步如下所示:

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"];
Dictionary lookup=cubes.ToDictionary(a=>a.Key,a=>a.Value);
十进制美元=查找[“美元”];

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