C# 从URL读取XML文件,结果为空

C# 从URL读取XML文件,结果为空,c#,xml,linq-to-xml,xmlreader,C#,Xml,Linq To Xml,Xmlreader,我正在尝试从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

我正在尝试从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);
        }
XDocument doc=XDocument.Load(“http://www.boi.org.il/currency.xml");
int currID=0;
字典货币;//词典声明
curr=新字典();
var data=from doc.subscriptions(“CURRENCY”)中的项//LINQ xml to data变量中的信息
选择新的
{
名称=项.元素(“名称”).值,
国家=项目元素(“国家”)值,
currencyCode=item.Element(“currencyCode”).Value,
比率=转换为双(项目元素(“比率”).Value),
单位=转换为双(项目元素(“单位”).值),
change=Convert.ToDouble(item.Element(“change”).Value),
};
foreach(var xn in data)//在foreach中对从xml读取的数据运行,并将其放入字典中的货币变量中
{
货币=新货币();
currency.Name=xn.Name;
currency.Country=xn.Country;
currency.CurrencyCode=xn.CurrencyCode;
货币.汇率=换算成双倍(xn.汇率);
currency.Unit=换算成双倍(xn.Unit);
currency.Change=Convert.ToDouble(xn.Change);
货币添加(货币ID、货币);
currID++;
}
foreach(当前的KeyValuePair条目)
{
Console.WriteLine(entry.Value.CurrencyCode);
}
我编辑了代码以查看输出,但什么也没有得到。 我做错了什么


提前感谢。

这里是代码的快速重构

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()方法一起使用

下面是代码的快速重构

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()方法一起使用

@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”为零。

@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”为零。

您在
foreach
中进行不需要的转换。您实际上根本不需要foreach或匿名类型。我10/10次得到数据,我不知道你为什么不。。。我正在运行它,只是打印出名字和国家。。。每次都很有效。是否存在异常,或者您有其他代码使此代码看起来好像没有运行?正如@Jonesy所说的。。。不需要转换两次。嗯。。数据变量的意义是什么?文档元素由元素组成。。所以应该是foreach(文档中的XElement elm),我没有删除XMLReader标记引用。。但是我看不到它在这个代码中被使用。。读卡器对象通常使用流。。Load()方法中肯定有一个流。。但它不在您的使用范围内。@BrettCaswell,我只编辑了一点代码,但仍然一无所获。打开了一个新项目,但仍然一无所获。它可能是某种带有cookies的东西吗?你在你的
foreach
中进行你不需要的转换。您实际上根本不需要foreach或匿名类型。我10/10次得到数据,我不知道你为什么不。。。我正在运行它,只是打印出名字和国家。。。每次都很有效。是否存在异常,或者您有其他代码使此代码看起来好像没有运行?正如@Jonesy所说的。。。不需要转换两次。嗯。。数据变量的意义是什么?文档元素由元素组成。。所以应该是foreach(文档中的XElement elm),我没有删除XMLReader标记引用。。但是我看不到它在这个代码中被使用。。读卡器对象通常使用流。。Load()方法中肯定有一个流。。但它不在您的使用范围内。@BrettCaswell,我只编辑了一点代码,但仍然一无所获。打开了一个新项目,但仍然一无所获。我建议使用XMLHttpRequest对象而不是使用响应流的原因是,您可以更好地说明站点的响应状态、内容类型等。。如果你需要我用一个代码示例来扩展它。。让我知道我是C#的新手,您能帮助我正确使用XMLHTTPRequest对象吗?我找到了几个例子,但不确定哪一个适合我的需要;xDoc.Load(“);XmlNodeList xmllist=xDoc.GetElementsByTagName(“货币”);Console.WriteLine(xmllist.Count);我得到的结果是“xmlsist.Count”为零。这可能是我的计算机造成的吗?我建议使用XMLHttpRequest对象,而不是使用响应流的原因是,您可以更好地说明站点的响应状态、内容类型等。如果您需要我用代码示例对此进行扩展。让我知道我对C非常陌生#,您能帮助正确使用XMLHTTPRequest对象吗?我找到了几个示例,但不确定其中哪一个适合我的需要。我也尝试了以下代码:XmlDocument xDoc=new XmlDocument();xDoc.Load(“);xmlnode