简体   繁体   中英

Restsharp xml Deserialization to list without changing the name of model

I have xml that is not very well formed, but need to map to a List with RestSharp. I do not have control of the service/ xml output. Thus far, I was able to get around issues with the properties themselves using the DeserializeAs(Name="name")] property. For instance,

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 correctly to a list when 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>

The function I am using is:

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

The problem is that I have xml that looks like this for a number of other requests,

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

Given it is not very descriptive xml, but I need to map this to a List.

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

https://stackoverflow.com/a/4082046/3443716 somewhat answers this, and it certainly works, but I do not want the model to be named row I tried to do this:

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

However RestSharp appers to ignore this attribute entirely. I have been searching a ton and found a few answers that suggest using a custom deserializer, but I have a hard time believing that is the only or easiest option for that matter. Is there some other attribute that I may be missing or is the only option using a custom deserializer?

As another note, I also tried to do something like this and I just get null 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; }
}

Thanks to this post, https://stackoverflow.com/a/27643726 I was able to "fork" the RestSharp Deserialzier and create a slightly custom one with the two line modification provided by The Muffin Man as follows

Added this to HandleListDerivative in the RestSharp.Deserializers.XmlDeserializer at line 344.

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

That allowed me to as desired add DeserializeAs as follows:

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

I am unsure why this is ignored by restsharp, this seems like it would be useful in a number of cases... As a side note, the functionality of creating nested lists is still available as well. Though I haven't run the tests after modification, it appears to do exactly what you would expect. Other than that all you have to do is add the custom handler to rest by callling Client.AddHandler("application/xml", new CustomXmlDeserializer());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM