Restsharp xml Deserialization to list without changing model name

I have an xml that is not well formed but needs to map a List to RestSharp. I have no control over the output of service / xml. So far I've managed to solve problems with the properties themselves using the property DeserializeAs(Name="name")]

. For example,

public class Physician
{
    [DeserializeAs(Name = "personId")]
    public string Id { get; set; }

    [DeserializeAs(Name = "fName")]
    public string FirstName { get; set; }

    [DeserializeAs(Name = "lName")]
    public string LastName { get; set; }
}

      

Maps are displayed correctly in the list if I have the following xml:

<data>
  <physician>
    <personId>3325</personId>
    <fName>Foo</fName>
    <lName>Bar</lName>
  </physician>
  <physician>
    <personId>3342</personId>
    <fName>Jane</fName>
    <lName>Doe</lName>
  </physician>
  ...
</data>

      

Function used:

public static List<T> GetListOfEntityType<T>(string url)
{
    return Client.Execute<List<T>>(new RestRequest(url)).Data;
}

      

The problem is I have an xml that looks like this for several other requests,

<data>
  <row>
    <typeId>0</typeId>
    <type>Physician</type>
  </row>
  <row>
    <typeId>1</typeId>
    <type>Ambulance</type>
  </row>
  ...
</data>

      

Considering this is not very descriptive xml, but I need to map this to List.

public class OrganizationType
{
    public string typeId { get; set; }
    public string type { get; set; }
}

      

This stackoverflow question has a few answers to this and it certainly works, but I don't want the model to be called row

. I tried to do this:

[DeserializeAs(Name = "row")]
public class OrganizationType
{
    public string typeId { get; set; }
    public string type { get; set; }
}

      

However, RestSharp users completely ignore this attribute. I've searched a ton and found several answers that suggest using a custom deserializer, but it's hard for me to believe this is the only or easiest option. Is there any other attribute that might be missing or is this the only option using a custom deserializer?

As another note, I also tried to do something like this and I'll just go back ...

public class OrganizationType
{
    public string typeId { get; set; }
    public string type { get; set; }
}

public class OrgTypeCollection
{
    [DeserializeAs(Name = "row")]
    public List<OrganizationType> Names { get; set; }
}

      

+3


source to share


1 answer


Thanks to this post to fooobar.com/questions/2225270 / ... I was able to "evolve" RestSharp Deserialzier and create a slightly custom variant with two line changes provided by The Muffin Man as follows

Added this to HandleListDerivative

in RestSharp.Deserializers.XmlDeserializer at line 344.

var attribute = t.GetAttribute<DeserializeAsAttribute>();
if (attribute != null) name = attribute.Name;

      



This allowed me to optionally add DeserializeAs

like this:

[DeserializeAs(Name = "row")]
public class OrganizationType
{
    public string typeId { get; set; }
    public string type { get; set; }
}

      

I'm not sure why this is being ignored by restsharp, it looks like it would be useful in a number of cases ... As an additional note, the nested list creation functionality is also available. Although I haven't run the tests after the modification, it seems to do exactly what you expect. Other than that, all you have to do is add a custom rest handler by calling Client.AddHandler("application/xml", new CustomXmlDeserializer());

+3


source







All Articles