如何反序列化 xml 以在 RestSharp 中列出?

问题描述:

我的 XML:

<result>
    <document version="2.1.0">
        <response type="currency">
            <currency>
                <code>AMD</code>
                <price>85.1366</price>
            </currency>
        </response>
        <response type="currency">
            <currency>
                <code>AUD</code>
                <price>31.1207</price>
            </currency>
        </response>
    </document>
</result>

我的班级:

public class CurrencyData
{
    public string Code { get; set; }
    public string Price { get; set; }
}

我的解串器调用:

RestClient.ExecuteAsync<List<CurrencyData>>...

如果我将 CurrencyData 类重命名为 Currency,那么一切都会正确完成.但我想保留这个类名.

If i renamed class CurrencyData to Currency then all will been done right. But I want to keep this class name.

好的,我想我明白了,

你可以试试RestClient.ExecuteAsync()

[XmlRoot("result")]
public class Result
{
    [XmlElement("document")]
    public Document Document { get; set; }
}

public class Document 
{
    [XmlElement("response")]
    public Response[] Responses { get; set; }

    [XmlAttribute("version")]
    public string Version { get; set; }
}

public class Response
{
    [XmlElement("currency")]
    public CurrencyData Currency { get; set; }

    [XmlAttribute("type")]
    public string Type { get; set; }
}

public class CurrencyData
{
    [XmlElement("code")]
    public string Code { get; set; }

    [XmlElement("price")]
    public decimal Price { get; set; }
}

我不得不添加一些 XmlElement 属性来覆盖大小写,而不必以小写形式命名类和属性.但是如果您可以更改 xml 以匹配大小写,则可以删除它们

I had to add a few XmlElement attribute to override the casing without having to name classes and properties in lowercase. but you can drop them if you can change the xml to match the casing