我如何使用Restsharp反序列化XML列表? [英] How can I deserialize Xml list using Restsharp?
问题描述
我有这样
<?xml version="1.0" encoding="utf-8"?>
<xml>
<item>
<accountid>1</accountid>
<accounttypeid>1</accounttypeid>
<accounttypename/>
<accountbankid>1</accountbankid>
<accountbankname/>
<accountsaldo>0</accountsaldo>
</item>
<item>
<accountid>2</accountid>
<accounttypeid>1</accounttypeid>
<accounttypename/>
<accountbankid>2</accountbankid>
<accountbankname/>
<accountsaldo>0</accountsaldo>
</item>
...
</xml>
我要反序列化这个XML列表POCO对象,它是
I want to deserialize this xml list to POCO object which is
public class Account
{
public string AccountId { get; set; }
public string AccountTypeId { get; set; }
public string AccountTypeName { get; set; }
public string AccountBankId { get; set; }
public string AccountBankName { get; set; }
public string AccountSaldo { get; set; }
}
我发现伟大的产品的 RestSharp 与其他客户合作。我想用它解串器,我试过2的方法。
I found great product RestSharp for working with rest client. I want to use its deserializer and I tried 2 approaches.
1),我试过
request.RootElement =项;
VAR响应=执行<帐户>(请求);
和我只拿到了第一个项目元素,它是合乎逻辑的。
and I only got first Item element which is logical.
2)我尝试类似
request.RootElement =XML;
VAR响应=执行<名单<帐户>>(请求);
我NULL。
我在哪里不对的。
更新:该解决方案是公认的答案评论
UPDATE: The solution is in accepted answer comments
推荐答案
它应该如果重命名帐户工作
类项目
,并使用执行<名单<项目>>(请求)
。你并不需要指定rootElement的值。
It should work if you rename the Account
class to Item
and use Execute<List<Item>>(request)
. You don't need to specify a RootElement value.
这篇关于我如何使用Restsharp反序列化XML列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!