9

I'm new to REST and this sounds like it should be pretty simple. In a .NET app, I can create a reference to a WCF service and the contracts for all the available types will be generated for me.

Now I'm trying to consume a REST service in a Windows Phone 7 app. While I can make my call and get back the proper response, is there a simple way to create the classes that each object would be deserialized to?

I'm using RestSharp to manage my calls. In some examples I've seen, user's have created their own classes, and generated the xml manually. I would like to avoid this if at all possible.

many thanks!

earthling
  • 5,084
  • 9
  • 46
  • 90

2 Answers2

9

Assuming your response is XML, you can save the xml into a file, then call xsd.exe on it to generate a schema. Call xsd.exe on the schema and it will generate a c# class file you can seriazlize and deserialize to from the xml. Here's the documeantion on how XSD.exe works:

http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=VS.100).aspx

chris.w.mclean
  • 1,793
  • 14
  • 17
3

You have to generate the classes that your response data will map to (or use a dynamic deserialization scheme if you're on .NET 4) since REST does not include a schema definition system the way SOAP does. In RestSharp, there's a T4 helper to make generating the C# classes easier. It gets you about 80% of the way there. If you need any help with it, post to the RestSharp Google Group.

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
John Sheehan
  • 77,456
  • 30
  • 160
  • 194
  • I had a very interesting discussion the other day with someone who suggested a way that this approach can be made self-descriptive, by using very specific rel values to identify to the client what precise type would be returned. The set of rels that a service supports would need to be clearly documented and supported by the client, but at least you can avoid coupling your URIs to return types this way. – Darrel Miller Feb 09 '11 at 02:43
  • 1
    https://github.com/restsharp/RestSharp/wiki/T4-Helper No idea if it still works though. That was a long time ago – John Sheehan Apr 26 '13 at 18:43