2013-02-14 76 views
0

無法在c#中讀取此xml。如何從這個XML如何在c#中讀取xml#

獲得貨幣和利率我用這個代碼

XmlTextReader xmlTextReader = new XmlTextReader(new StringReader(xmlfile)); 
XmlDocument doc = new XmlDocument(); 
XmlNode node = doc.ReadNode(xmlTextReader); 

,但沒有得到的值。

<?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='2013-02-14'> 
     <Cube currency='USD' rate='1.3327'/> 
     <Cube currency='JPY' rate='124.39'/> 
     <Cube currency='BGN' rate='1.9558'/> 
     <Cube currency='CZK' rate='25.383'/> 
     <Cube currency='DKK' rate='7.4604'/> 
     <Cube currency='GBP' rate='0.85940'/> 
     <Cube currency='HUF' rate='292.52'/> 
     <Cube currency='LTL' rate='3.4528'/> 
     <Cube currency='LVL' rate='0.6997'/> 
     <Cube currency='PLN' rate='4.1765'/> 
     <Cube currency='RON' rate='4.3871'/> 
     <Cube currency='SEK' rate='8.4492'/> 
     <Cube currency='CHF' rate='1.2293'/> 
     <Cube currency='NOK' rate='7.3605'/> 
     <Cube currency='HRK' rate='7.5863'/> 
     <Cube currency='RUB' rate='40.1712'/> 
     <Cube currency='TRY' rate='2.3605'/> 
     <Cube currency='AUD' rate='1.2879'/> 
     <Cube currency='BRL' rate='2.6220'/> 
     <Cube currency='CAD' rate='1.3343'/> 
     <Cube currency='CNY' rate='8.3062'/> 
     <Cube currency='HKD' rate='10.3352'/> 
     <Cube currency='IDR' rate='12873.40'/> 
     <Cube currency='ILS' rate='4.9036'/> 
     <Cube currency='INR' rate='71.8730'/> 
     <Cube currency='KRW' rate='1445.93'/> 
     <Cube currency='MXN' rate='16.9523'/> 
     <Cube currency='MYR' rate='4.1170'/> 
     <Cube currency='NZD' rate='1.5715'/> 
     <Cube currency='PHP' rate='54.251'/> 
     <Cube currency='SGD' rate='1.6491'/> 
     <Cube currency='THB' rate='39.728'/> 
     <Cube currency='ZAR' rate='11.8588'/> 
    </Cube> 
</Cube> 

+1

請不要使用'new XmlTextReader()'。從.NET 2.0開始已經被棄用了。改用'XmlReader.Create()'。 – 2013-02-14 20:21:20

回答

2

你總是可以做的XML閱讀使用XPath但我強烈建議你使用LINQ to XML(.NET 3.5),如果可能的話:

XDocument doc = XDocument.Load(url); 
XNamespace gesmes = "http://www.gesmes.org/xml/2002-08-01"; 
XNamespace ns = "http://www.ecb.int/vocabulary/2002-08-01/eurofxref"; 
var cubes = doc.Descendants(ns + "Cube") 
       .Where(x => x.Attribute("currency") != null) 
       .Select(x => new { Currency = (string) x.Attribute("currency"), 
            Rate = (decimal) x.Attribute("rate") }); 
foreach (var result in cubes) 
{ 
    Console.WriteLine("{0}: {1}", result.Currency, result.Rate); 
} 

cubes存儲在您需要值即CurrencyRate

+0

@JonSkeet你一直是這個答案的背後......! – 2013-02-14 20:16:43

0

爲了獲得某個節點的值或屬性,首先需要找到該節點。
的傳統方法:

string query = "//Cube[@currency and @rate]";//select the cube node that has "currency" and "rate" attributes. 
XmlNodeList nodes = doc.SelectNodes(query); 
foreach (XmlNode node in nodes) 
{ 
    string currency = node.Attributes["currency"].Value; 
    string rate = node.Attributes["rate"].Value; 
    Console.WriteLine("currency: {0}, rate: {1}", currency, rate); 
} 

希望這是有幫助的。