2014-07-25 107 views
1

我正在嘗試從URL中讀取XML文件。
URL和文件都很好,它們持有貨幣匯率。從URL讀取XML文件,出現空

10次運行代碼9時,沒有內容返回。

下面是代碼:

 XDocument doc = XDocument.Load("http://www.boi.org.il/currency.xml"); 
     int currID = 0; 
     Dictionary<int, Currency> curr; // declares the dictionary 
     curr = new Dictionary<int, Currency>(); 

     var data = from item in doc.Descendants("CURRENCY") // LINQ the informartion from the xml to data variable 
        select new 
        { 
         name = item.Element("NAME").Value, 
         country = item.Element("COUNTRY").Value, 
         currencyCode = item.Element("CURRENCYCODE").Value, 
         rate = Convert.ToDouble(item.Element("RATE").Value), 
         unit = Convert.ToDouble(item.Element("UNIT").Value), 
         change = Convert.ToDouble(item.Element("CHANGE").Value), 
        }; 

     foreach (var xn in data) // run in foreach on the data that we read from the xml and put it in a currency variable into the dictionary 
     { 
      Currency currency = new Currency(); 

      currency.Name = xn.name; 
      currency.Country = xn.country; 
      currency.CurrencyCode = xn.currencyCode; 
      currency.Rate = Convert.ToDouble(xn.rate); 
      currency.Unit = Convert.ToDouble(xn.unit); 
      currency.Change = Convert.ToDouble(xn.change); 

      curr.Add(currID, currency); 
      currID++; 
     } 

     foreach (KeyValuePair<int, Currency> entry in curr) 
     { 
      Console.WriteLine(entry.Value.CurrencyCode); 
     } 

我已編輯的代碼,看看輸出,我什麼也沒得到。 我在做什麼錯?

在此先感謝。

+0

你正在做你不需要的'foreach'中的轉換。你根本不需要「foreach」或匿名類型。我得到數據10/10次我打了服務,我不知道你爲什麼不會 – Jonesopolis

+0

同上...我正在運行它,只是打印出名字和國家......每次都有效。有沒有例外,或者你有其他代碼,使它看起來像這個代碼沒有運行?另外@Jonesy說...不需要轉換兩次。 – Kevin

+0

嗯..數據變量的要點是什麼? doc.Elements包含XElements ..所以它應該只是foreach(XElement elm in doc) –

回答

4

@David Faiz It Works!

XmlDocument xDoc = new XmlDocument(); 
    xDoc.Load(@"http://www.boi.org.il//currency.xml"); 
    XmlNodeList xmllist = xDoc.GetElementsByTagName("CURRENCIES"); 
    Console.WriteLine(xmllist.Count); 

你必須添加斜線//在URL。這就是爲什麼你把'xmllist.Count'作爲零。

+0

哦對..完全忘了c#需要.. –

+0

謝謝你的幫助! –

+0

@jai 嘿,我已經跑了代碼,它正確地返回了xml,但是每隔一段時間。我再一次,什麼也得不到。 任何想法爲什麼? –

1

這是你的代碼的快速重構..

XDocument doc = XDocument.Load(@"http://www.boi.org.il/currency.xml"); 

foreach (XElement elm in doc.Elements) 
{ 
    Currency currency = new Currency(); 

    currency.Name = elm.Element("NAME").Value; 
    currency.Country = elm.Element("COUNTRY").Value; 
    currency.CurrencyCode = elm.Element("CURRENCYCODE").Value; 
    currency.Rate = Convert.ToDouble(elm.Element("RATE").Value); 
    currency.Unit = Convert.ToDouble(elm.Element("UNIT").Value); 
    currency.Change = Convert.ToDouble(elm.Element("CHANGE").Value); 

    MessageBox.Show(elm.Element("CURRENCYCODE").Value); 

    curr.Add(currID, currency); 
    currID++; 
} 

但是,我不知道這解決您遇到的根本問題..

你可以包括System.Net命名空間並初始化一個XMLHttpRequest對象,並使用帶有靜態XDocument.Load()方法的Response流..

+0

我建議使用XMLHttpRequest對象而不是使用響應流的原因是因爲您可以更好地解釋網站的響應狀態,contentType等。如果您需要我使用代碼示例來擴展該功能。請讓我知道 –

+0

我對C#超級新手,你能否幫助正確使用XMLHTTPRequest對象? 我發現了幾個例子,但不知道哪一個符合我的需求。 –

+0

我也試過這段代碼: XmlDocument xDoc = new XmlDocument(); xDoc.Load(「http://www.boi.org.il/currency.xml」); XmlNodeList xmllist = xDoc.GetElementsByTagName(「CURRENCY」); Console.WriteLine(xmllist.Count); 我回來說'xmllist.Count'是零。 它可以是我的電腦的東西? –