I've been struggling with getting RestSharp to parse the following returned XML data:
<?xml version="1.0" encoding="UTF-8"?>
<TICKETANYWHERE>
<COUPON VER="1.0">
<RESPONSE>
<TEMPLATELIST>
<TEMPLATE ID="000000001">
<NAME>Some name</NAME>
</TEMPLATE>
<TEMPLATE ID="000000001">
<NAME>Some other name</NAME>
</TEMPLATE>
</TEMPLATELIST>
</RESPONSE>
</COUPON>
</TICKETANYWHERE>
It's returned from an API, so I have no way to change this XML. I have created the following classes to model the XML file:
public class Template
{
public string Id { get; set; }
public string Name { get; set; }
}
public class TemplateList : List<Template>
{}
public class Response
{
public TemplateList Templates { get; set; }
}
public class Coupon
{
public decimal Ver { get; set; }
public Response Response { get; set; }
}
public class TicketAnywhere
{
public Coupon Coupon { get; set; }
}
And then I have created the following unittest (inspired by)
var d = new XmlDeserializer();
var response = new RestSharp.RestResponse();
var XML = "<TICKETANYWHERE><COUPON VER=\"1.0\"><RESPONSE><TEMPLATELIST><TEMPLATE ID=\"00000001\"><NAME>Some name</NAME></TEMPLATE></TEMPLATELIST></RESPONSE></COUPON></TICKETANYWHERE>";
response.Content = XML;
var r = d.Deserialize<TicketAnywhere>(response);
However, r.Coupon is NULL. What am I doing wrong here?
UPDATE: I tried to boil down the problem to it's most simple form by changing the XML and the test to the following:
public class Template
{
public string Id { get; set; }
public string Name { get; set; }
}
public class TemplateList
{
public List<Template> Templates { get; set; }
}
[Test]
public void AnotherTest()
{
var XML = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<TEMPLATELIST>
<TEMPLATE ID=""00000001"">
<NAME>Some name</NAME>
</TEMPLATE>
<TEMPLATE ID=""00000002"">
<NAME>Some name</NAME>
</TEMPLATE>
</TEMPLATELIST>";
var d = new XmlDeserializer();
var response = new RestSharp.RestResponse();
response.Content = XML;
var r = d.Deserialize<TemplateList>(response);
Assert.That(2, Is.EqualTo(r.Templates.Count));
}
Which again fails. However, if I change the class name from Template => TEMPLATE it all seems to work.