【问题标题】:Read XML file from URL, comes up empty从 URL 读取 XML 文件,结果为空
【发布时间】:2014-09-17 17:29:39
【问题描述】:

我正在尝试从 URL 读取 XML 文件。
URL 和文件很好,它们包含货币汇率。

当代码运行 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);
        }

我已经编辑了代码以查看输出,但我什么也没得到。 我做错了什么?

提前致谢。

【问题讨论】:

  • 您正在您的foreach 中进行不需要的转换。你真的根本不需要foreach 或匿名类型。我获得了 10/10 次服务的数据,我不知道你为什么不这样做
  • 同上...我正在运行它,只是打印出名称和国家...每次都有效。是否存在异常,或者您可能有其他代码使该代码看起来没有运行?同样正如@Jonesy 所说......无需转换两次。
  • hmm.. 数据变量的意义何在? doc.Elements 由 XElements 组成.. 所以它应该只是 foreach (XElement elm in doc)
  • 我没有删除 XMLReader 标记引用.. 但我没有看到它在此代码中使用.. Reader 对象通常使用流.. 负载中肯定存在流() 方法..但它不在你这里的使用范围内..
  • @BrettCaswell,我对我的代码进行了一些编辑,但仍然一无所获。打开一个新项目,仍然没有。可以是某种 cookie 吗?

标签: c# xml linq-to-xml xmlreader


【解决方案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() 方法一起使用。

【讨论】:

  • 我建议使用 XMLHttpRequest 对象而不是使用响应流的原因是因为您可以更好地考虑站点的响应状态、contentType 等。如果您需要我用代码对此进行扩展样品..让我知道
  • 我是 C# 的超级新手,您能帮忙正确使用 XMLHTTPRequest 对象吗?我找到了几个例子,但不确定哪一个适合我的需要。
  • 我也试过这个代码:XmlDocument xDoc = new XmlDocument(); xDoc.Load("boi.org.il/currency.xml"); XmlNodeList xmllist = xDoc.GetElementsByTagName("CURRENCY"); Console.WriteLine(xmllist.Count); 我回来说 'xmllist.Count' 为零。这可能是我电脑的问题?
【解决方案2】:

@David Faiz 成功了!

    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'设为零。

【讨论】:

  • @jai 嘿,我已经运行了代码,它正确地返回了 xml,但是每隔一次。我再一次,一无所获。知道为什么吗?
  • @jai,你在这里逃了两次吧? @//currency.xml.. 是错字吧?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
相关资源
最近更新 更多