我正在尝试在 C# 中解析以下 XML 结构。我想用货币创建一个 List>。
<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2013-09-27">
<Cube currency="USD" rate="1.3537"/>
<Cube currency="JPY" rate="133.28"/>
<Cube currency="BGN" rate="1.9558"/>
<Cube currency="CZK" rate="25.690"/>
<Cube currency="DKK" rate="7.4573"/>
(....)
我试过使用 XDocument.Descendants,但它没有返回任何东西。我猜这与 Cube-element 用于多个级别的事实有关。
XDocument xdoc = XDocument.Parse(xml);
var currencies = from cube in xdoc.Descendants("Cube")
select new
{
Currency = cube.Attribute("currency").Value,
Rate = cube.Attribute("rate").Value
};
foreach (var currency in currencies)
this.Add(new KeyValuePair<string, double>(currency.Currency, Convert.ToDouble(currency.Rate)));
如何解析 XML 结构以获取货币?