1

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.

Community
  • 1
  • 1
nover
  • 2,259
  • 1
  • 27
  • 27

1 Answers1

1

XML is case-sensitive in the deserialization process. To quickly get around this you can try

XML = XML.Replace("COUPON", "Coupon");

etc for each of your element names.

If you're going to be processing many different xml files then it will be worth finding something that will do that nicely for you.

nb, you could also change the name of your property in Template from Coupon to COUPON

wal
  • 17,409
  • 8
  • 74
  • 109
  • Thanks for clarifying the case-sensitivity. Please see my edit of the question. Since this is API returned XML I'm really in no position to replace content, but, if capitalizing my class names solves the problem, I fear this is they way to go, even though it's UGLY. thanks for the help!! :) – nover Jun 27 '13 at 12:34
  • There is a way to decorate your c# classes with attributes using the `DeserializeAs` attribute however I havent got this working yet so cant paste that as an alternative answer yet. – wal Jun 27 '13 at 12:35
  • OK. But it actually _does seem_ to work with renaming both the classes and properties to CAPS. Should I post that as an alternate answer, or should the credit go to you (you creating an alternate answer)?? – nover Jun 27 '13 at 12:41
  • ok sorry I renamed the class, not the property. I'll add that as an answer. You only need to rename the property name, not the class. That way at least your code is SHOUTING at you a little less. – wal Jun 27 '13 at 12:45
  • Hmm, it seems like RestSharp is using the literal xml name (TEMPLATE) to also resolve the class. If I don't have both Property and Classname in CAPS, my unit test fails. I guess I just have to leave my code shouting at me... – nover Jun 27 '13 at 13:03
  • That also seems to go for the properties on my TEMPLATE class, all in caps. Can it really be that RestSharp does not perform fuzzy matching on this? – nover Jun 27 '13 at 13:18
  • @nover Lowercase class name works for me. see this image dump: http://imgur.com/zM0OB8t my mouse is over the `result` variable. showing Coupon is defined. And yes you will need it on ALL properties, i've looked at the restsharp source and it has some 'matching' that allows lowercase in the xml to match properties in your C# property names but not uppercase. – wal Jun 27 '13 at 23:38
  • Interesting that it works for you with normally capitalized class names - anyway this thread got me going and I have the API integration in place now, so thanks for your efforts again. – nover Jun 28 '13 at 06:53