如何反序列化 xml 以在 RestSharp 中列出? [英] how to deserialize xml to list in RestSharp?

查看:39
本文介绍了如何反序列化 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

这篇关于如何反序列化 xml 以在 RestSharp 中列出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆