开发者

XML to JSON or To where to use it in .NET c#

How can I gather the data into c# from this xml? I get the data from :http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

PS: I would like to get the currencies rates from a web service, bu I could not find a trustworthy web service, thats why I try from 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 curren开发者_运维百科cy="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>


The first steps would look like:

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

And maybe convert it like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜