指向 c# 对象的 XML URL



>我有以下链接:XML 文件

如您所见,它有一个XML文件,其中包含一个名为Currency的对象,其中包含货币列表。

我有货币类,我需要将该"货币"列表提取到一个对象中 "list<Currency> allCurrencies=...."

这是一个 UWP 项目。最好没有异步的东西,而且由于某种原因,我没有 WebClient 类的 dll。我尝试了很多方法,但总是失败,有各种各样的例外。

使用 XML Linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        const string URL = "http://www.boi.org.il/currency.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(URL);
            Currency.currencies = doc.Descendants("CURRENCY").Select(x => new Currency() {
                name = (string)x.Element("NAME"),
                unit = (int)x.Element("UNIT"),
                code = (string)x.Element("CURRENCYCODE"),
                country = (string)x.Element("COUNTRY"),
                rate = (decimal)x.Element("RATE"),
                change = (decimal)x.Element("CHANGE")
            }).ToList();
        }
    }
    public class Currency
    {
        public static List<Currency> currencies = new List<Currency>();
        public string name { get; set; }
        public int unit { get; set; }
        public string code { get; set; }
        public string country { get; set; }
        public decimal rate { get; set; }
        public decimal change { get; set; }
    }
}

最新更新