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