2015-09-10 43 views
-1

我想閱讀加拿大銀行的XML響應以獲得各種貨幣的收盤匯率。 但是他們有很複雜的XML響應,你可以在如何使用C#讀取複雜的XML響應#

訪問www.bankofcanada.ca/stats/assets/rates_rss/closing/en_all.xml檢查

我試圖解析XML我的舊方式,但這項工作的只有簡單的XML即精細

var url ="http://www.bankofcanada.ca/stats/assets/rates_rss/closing/en_all.xml"; 
     var uploadResult = CurrencyAPI.TriggerApiAction(url); 
     XDocument xDoc = XDocument.Parse(CurrencyAPI.backstr); 



     foreach (var download in xDoc.Descendants("cb:exchangeRate")) 
     { 
      a1 = download.Element("cb:value").Value; 
      a2 = download.Element("cb:baseCurrency").Value; 
      a3 = download.Element("cb:targetCurrency").Value; 
     } 

請幫我這個 感謝

回答

0
XNamespace cb = "http://www.cbwiki.net/wiki/index.php/Specification_1.1"; 
foreach (var download in xDoc.Descendants(cb+"exchangeRate")) 
{ 
    var a1 = download.Element(cb+"value").Value; 
    var a2 = download.Element(cb+"baseCurrency").Value; 
    var a3 = download.Element(cb+"targetCurrency").Value; 
} 

或者更好:

XNamespace cb = "http://www.cbwiki.net/wiki/index.php/Specification_1.1"; 
foreach (var download in xDoc.Descendants(cb+"exchangeRate")) 
{ 
    var a1 = (decimal)download.Element(cb+"value"); 
    var a2 = (string)download.Element(cb+"baseCurrency"); 
    var a3 = (string)download.Element(cb+"targetCurrency"); 
} 
+0

謝謝你的工作 –