How can I deserialize Xml list using Restsharp?

筅森魡賤 提交于 2019-11-29 09:15:27

问题


I have an xml like this

<?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> 

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; } 
} 

I found great product RestSharp for working with rest client. I want to use its deserializer and I tried 2 approaches.

1) I tried

request.RootElement = "item";

var response = Execute<Account>(request);

and I only got first Item element which is logical.

2) When I try something like

request.RootElement = "xml";

var response = Execute<List<Account>>(request);

I got null.

Where am I wrong with this?

UPDATE: The solution is in accepted answer comments


回答1:


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.




回答2:


Not sure what's wrong, but I'm sure John will be by soon to let you know :-) In the meanwhile, why not just do it the manual way:

    var root = XElement.Parse(xmlString);
    var accounts = from it in root.Element("xml").Elements("item")
                   select new Account() {
                                            AccountId = it.Element("accountid").Value,
                                            AccountTypeId = it.Element("accounttypeid").Value,
                                            AccountTypeName = it.Element("accounttypename").Value,
                                            AccountBankId = it.Element("banktypeid").Value,
                                            AccountBankName = it.Element("banktypename").Value,
                                            AccountSaldo = it.Element("accountsaldo").Value
                                        };

It's so clean and easy with XLinq. By adding a few extension methods to XElement you can make it even cleaner and resilient to missing elements/attributes.



来源:https://stackoverflow.com/questions/4077850/how-can-i-deserialize-xml-list-using-restsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!