9

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

nemke
  • 2,440
  • 3
  • 37
  • 57

2 Answers2

9

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.

John Sheehan
  • 77,456
  • 30
  • 160
  • 194
  • Hmm, I got ErrorMessage = "Index was outside the bounds of the array." – nemke Nov 02 '10 at 22:26
  • Does it work if you set `RootElement = "xml";`? I thought it worked without that, but maybe I'm remembering incorrectly. – John Sheehan Nov 02 '10 at 22:36
  • and if that doesn't work, try setting RootElement and naming the class `item` – John Sheehan Nov 02 '10 at 22:41
  • Nope, it won't work. I tried both of the cases. Maybe I should pull from the github and create test for this case (adding the sample xml) – nemke Nov 02 '10 at 23:10
  • I'll try it out tonight and figure out what's going on. – John Sheehan Nov 02 '10 at 23:28
  • 5
    Ok, this is how I did it, couldn't go to sleep :) I created `public class XmlItems : List { } and then Execute(request) – nemke Nov 03 '10 at 00:51
  • That was going to be my next suggestion :) I thought RootElement + `List` would work. It probably should. If you submit a test, I will be motivated to add it. – John Sheehan Nov 03 '10 at 00:54
  • Why is it necessary to derive from List into a concrete class? Is there a way to get this done while still using a generic list? – urig Mar 07 '17 at 14:55
6

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.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • Yes, I'm fine with linq2xml but I wanted to create client api for fast development, without the need for manual coding. I need a working vertical to show to my colleagues. 10x for the answer anyway! – nemke Nov 02 '10 at 15:37