Testing RestSharp deserialization without proper REST-Api

EDIT: A solution to the question can be found in John Sheehan's first comment!

I would like to use Restsharp as a Rest-Client for my project. Since the REST server is not running yet, I would like to test the client without the server. The focus is on deserializing the returned XML response. Is it possible to deserialize XML using RestSharp without proper RestSharp.RestResponse?

I tried like this:

public void testDeserialization() { XmlDeserializer d = new XmlDeserializer(); RestSharp.RestResponse response = new RestSharp.RestResponse(); string XML = @"<Response><Item1>Some text</Item1><Item2>Another text</Item2><Item3>Even more text</Item3></Response>"; response.Content = XML; d.RootElement = "Response"; Response r = d.Deserialize<Response>(response); } public class Response { public string Item1 { get; set; } public string Item2 { get; set; } public string Item3 { get; set; } } 

Deserializations creates an object of the Response-Class class, where each field is null. Is there a way to check if (and how) any given xml will be deserialized by RestSharp?

Edit: For better readability, this is the XML I use:

 <Response> <Item1>Some text</Item1> <Item2>Another text</Item2> <Item3>Even more text</Item3> </Response> 
+4
source share
1 answer

I hope that I am doing it right, but in order to resolve this issue, I copy the solutions (according to the comments of John Sheehan):

You do not need to specify a RootElement. This is only when root is not at the top level. Try this and let me know if that works. Here's how we test the deserializer for the project: https://github.com/restsharp/RestSharp/blob/master/RestSharp.Tests/XmlDeserializerTests.cs

(EDIT: updated link for the correct file)

+6
source

Source: https://habr.com/ru/post/1391471/


All Articles