C# 将 XML 汇率解析为欧洲央行的字典

C# Parse XML exchange rates into dictionary from ECB

使用来自欧洲中央银行的 URL:

www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml

我想将货币符号和汇率导入到字典或对象中。我已经读入了 xml 文档,但我在选择节点属性时遇到了问题。

谢谢

string xmlString;
        using (var client = new WebClient())
        {
            xmlString = client.DownloadString("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
        }

        var xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlString);

        foreach(XmlNode node  in xmlDoc.SelectNodes("//*/Cube/@currency"))
        {
            // add currency and rate to dictionary
        }
class Program
{
    static void Main(string[] args)
    {
        List<Rate> rates = new List<Rate>();

        var doc = new XmlDocument();
        doc.Load(@"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");

        XmlNodeList nodes = doc.SelectNodes("/*/*/*/*");

        for (int i = 0; i < nodes.Count; i++)
        {
            var rate = new Rate()
            {
                Currency = nodes[i].Attributes[0].Value,
                Value = Decimal.Parse(nodes[i].Attributes[1].Value)
            };

            rates.Add(rate);
        }
        Console.WriteLine();
    }
}

class Rate
{
    public string Currency { get; set; }
    public decimal Value { get; set; }
}
        string xmlString;
        using (var client = new WebClient())
        {
            xmlString = client.DownloadString("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
        }

    var doc = XDocument.Parse(xmlString);
    XNamespace ns = "http://www.ecb.int/vocabulary/2002-08-01/eurofxref";
    var values = doc
        .Root
        .Element(ns + "Cube")             
        .Element(ns + "Cube")
        .Elements(ns + "Cube")
        .ToDictionary(e => e.Attribute("currency"), e => (double) e.Attribute("rate"));

If the XPath expression does not include a prefix, it is assumed that the namespace URI is the empty namespace. If your XML includes a default namespace, you must still add a prefix and namespace URI to the XmlNamespaceManager; otherwise, you will not get any nodes selected.

使用这个重载 XmlNode.SelectNodes(String, XmlNamespaceManager).

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("ecb", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
foreach (XmlNode node in xmlDoc.SelectNodes("//ecb:Cube[@currency]", nsmgr))

我认为问题出在您的 xPath select或。

"//*[@currency]" 的值将 select 属性为 "currency"

的所有元素
class Program
{
    public static void Main(string[] args)
    {
        List<Rate> rates = new List<Rate>();

        var doc = new XmlDocument();
        doc.Load(@"http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");

        XmlNodeList nodes = doc.SelectNodes("//*[@currency]");

        if (nodes != null)
        {
            foreach (XmlNode node in nodes)
            {
                var rate = new Rate()
                           {
                              Currency = node.Attributes["currency"].Value,
                              Value = Decimal.Parse(node.Attributes["rate"].Value, NumberStyles.Any, new CultureInfo("en-Us"))
                           };
                rates.Add(rate);
            }
        }
    }
}
class Rate
{
    public string Currency { get; set; }
    public decimal Value { get; set; }
}