C#反序列化格式奇怪的XML

时间:2018-03-22 18:26:28

标签: c# xml deserialization

我花了最后一天在这里阅读所有关于XML deseralization的热门帖子,虽然我对示例文件没有任何问题,但是我不能将它应用到我的特定文件(授权,也许格式很好,我太缺乏经验了):

<?xml version="1.0" encoding="UTF-8"?>
<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='2018-03-20'>
            <Cube currency='USD' rate='1.2276'/>
            <Cube currency='JPY' rate='130.72'/>
            <Cube currency='BGN' rate='1.9558'/>
            <Cube currency='CZK' rate='25.423'/>
            <Cube currency='DKK' rate='7.4485'/>
            <Cube currency='GBP' rate='0.87715'/>
            <Cube currency='HUF' rate='311.13'/>
            <Cube currency='PLN' rate='4.2277'/>
            <Cube currency='RON' rate='4.6663'/>
            <Cube currency='SEK' rate='10.0563'/>
            <Cube currency='CHF' rate='1.1721'/>
            <Cube currency='ISK' rate='122.50'/>
            <Cube currency='NOK' rate='9.4863'/>
            <Cube currency='HRK' rate='7.4423'/>
            <Cube currency='RUB' rate='70.8466'/>
            <Cube currency='TRY' rate='4.8238'/>
            <Cube currency='AUD' rate='1.5934'/>
            <Cube currency='BRL' rate='4.0410'/>
            <Cube currency='CAD' rate='1.6040'/>
            <Cube currency='CNY' rate='7.7744'/>
            <Cube currency='HKD' rate='9.6295'/>
            <Cube currency='IDR' rate='16893.01'/>
            <Cube currency='ILS' rate='4.2741'/>
            <Cube currency='INR' rate='80.0370'/>
            <Cube currency='KRW' rate='1314.93'/>
            <Cube currency='MXN' rate='23.0086'/>
            <Cube currency='MYR' rate='4.8091'/>
            <Cube currency='NZD' rate='1.7039'/>
            <Cube currency='PHP' rate='63.926'/>
            <Cube currency='SGD' rate='1.6174'/>
            <Cube currency='THB' rate='38.301'/>
            <Cube currency='ZAR' rate='14.6788'/>
        </Cube>
    </Cube>
</gesmes:Envelope>

我想获取最里面的多维数据集节点列表来访问货币值。 有两件事让我感到困惑:1。有3个级别的立方体,都有相同的名称,2。肥皂信封的名称空间不适用于立方体本身。

我尝试了XDocument路由并提供了命名空间,但是yielding list只包含空项:

string currencies;

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

XDocument xDoc = XDocument.Load(new StringReader(currencies));
XNamespace soap = XNamespace.Get("http://www.gesmes.org/xml/2002-08-01");

我发现的所有例子都非常强调提供命名空间,所以我怀疑问题就在那里。我不需要在3年内反序列化,所以我对这个主题非常生疏,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您的问题可能与使用xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref"指定的默认命名空间有关? Cube元素位于此命名空间中,您需要在查询XML时指定:

var xDocument = XDocument.Parse(xml);
var ns = (XNamespace) "http://www.ecb.int/vocabulary/2002-08-01/eurofxref";
var currencies = xDocument
    .Root
    .Element(ns + "Cube")
    .Element(ns + "Cube")
    .Elements(ns + "Cube")
    .Select(xElement => new
    {
        Currency = (string) xElement.Attribute("currency"),
        Rate = (decimal) xElement.Attribute("rate")
    })
    .ToList();

这将创建一个包含费率的货币列表。我使用了匿名类型,但您可以使用自己的类型。