简体   繁体   中英

Testing the Deserialization of RestSharp without proper REST-Api

EDIT: The solution to the question can be found in the first comment by John Sheehan!

i would like to use Restsharp as Rest-Client for my Project. Since the REST server is not running yet, I would like to test the client without the Server. My main focus is on the deserialization of the returning XML-Response. Is it possible to deserialize XML using RestSharp without a proper RestSharp.RestResponse?

I tried it 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; }
}

The deserializations creates an Object of the Response-Class, where every field is null. Is there a way to test if (and how) any given xml would be deserialized by RestSharp?

Edit: For better readability - this is the XML i'm using:

<Response>
    <Item1>Some text</Item1>
    <Item2>Another text</Item2>
    <Item3>Even more text</Item3>
</Response>

I hope I'm doing this right - but to make clear this question is solved, i'm copying the solutions (from the comments by John Sheehan):

You shouldn't have to specify RootElement. That's only for when the root isn't at the top level. Try that and let me know if it 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 to correct file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM