2

I am trying to deserialize this to an c# object using restsharp

Before marking as a duplicate question note that I have looked at the following stack overflow questions and it has not worked

The response:

<response>
    <TAG>
         <tagid>1</tagid>
         <mac>00:12:8E:12:2F:34</mac>
    </TAG>
    <TAG>
         <tagid>2</tagid>
         <mac>00:11:8E:12:3F:11</mac>
    </TAG>
</response>

My Class that I hoped to use but is only it states count =0...

public class Response
{
    public Response()
    {
        this.Tag = new List<Tag>();
    }
    [XmlElement()]
    public List<Tag> Tag { get; set; } 
}

public class Tag
{
    public string Tagid { get; set; }
    public string Mac { get; set; }
}

The code that tries to deserialize

var request = new RestRequest
              {
                  Resource = "api/notimportant",
                  RequestFormat = DataFormat.Xml,
                  Method = Method.GET,
              };
request.AddParameter("fields", "all");

var client = new RestClient
             {
                 BaseUrl = new Uri(_url),
             }; 
client.AddHandler("application/xml", new DotNetXmlDeserializer());

var response = client.Execute<Response>(request);

My many test

If I used response I get nothing but when I used tag I get only the first element instead of list which I what I want...

enter image description here enter image description here

Community
  • 1
  • 1
hidden
  • 3,216
  • 8
  • 47
  • 69

1 Answers1

2

It seems to be partially case sensitive. The <TAG> element being all caps is throwing it off. I finally got it to work when I changed the classes to this:

public class Response
{
    public Response()
    {
        this.Tags = new List<TAG>();
    }
    public List<TAG> Tags { get; set; }
}

public class TAG
{
    public string Tagid { get; set; }
    public string Mac { get; set; }
}

Without the actual rest API to call, here's what I did to test it:

var d = new XmlDeserializer();
var response = new RestSharp.RestResponse();
response.Content = @"<response>
<TAG>
     <tagid>1</tagid>
     <mac>00:12:8E:12:2F:34</mac>
</TAG>
<TAG>
     <tagid>2</tagid>
     <mac>00:11:8E:12:3F:11</mac>
</TAG>
</response>";
var result = d.Deserialize<Response>(response);
CoderDennis
  • 13,642
  • 9
  • 69
  • 105
  • You may be able to use the `DeserializeAs` attribute although I'm not too sure if it will work. – Teeknow Mar 19 '15 at 23:37
  • 1
    @Frontenderman I was looking for something like that, but when I tried adding `[DeserializeAs(Name = "TAG")]` to the Tags property it produced a NullReferenceException. – CoderDennis Mar 20 '15 at 00:56
  • the restsharp documentation lied:https://github.com/restsharp/RestSharp/wiki/Deserialization... ok i will try to look for a way to make DeserializeAs work so that I can follow my naming convention. – hidden Mar 20 '15 at 03:32
  • The weirdest part of all this is that if you use Tag without capitalizing it restsharp returns the first element. But if you nest it with response... restsharp can't handle it. Which means that restsharp did not take care of that scenario. – hidden Mar 20 '15 at 04:09
  • Update: i was unable to make deserializeAs to work. Had to write ugly classes (not following my naming convention) – hidden Jun 03 '15 at 15:25