在没有适当的REST-Api的情况下测试RestSharp的反序列化

时间:2022-10-13 15:50:42

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

编辑:问题的解决方案可以在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?

我想在我的项目中使用Restsharp作为Rest-Client。由于REST服务器尚未运行,我想在没有服务器的情况下测试客户端。我主要关注的是返回XML-Response的反序列化。是否可以在没有适当的RestSharp.RestResponse的情况下使用RestSharp反序列化XML?

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?

反序列化创建了一个Response-Class的Object,其中每个字段都为null。有没有办法测试RestSharp是否(以及如何)对任何给定的xml进行反序列化?

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

编辑:为了更好的可读性 - 这是我正在使用的XML:

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

1 个解决方案

#1


6  

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):

我希望我做得对 - 但是为了弄清楚这个问题已经解决了,我正在复制解决方案(来自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

您不必指定RootElement。这只适用于root不在*的情况。尝试一下,让我知道它是否有效。以下是我们测试该项目的反序列化器的方法:https://github.com/restsharp/RestSharp/blob/master/RestSharp.Tests/XmlDeserializerTests.cs

(EDIT: Updated link to correct file)

(编辑:更新了正确文件的链接)

#1


6  

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):

我希望我做得对 - 但是为了弄清楚这个问题已经解决了,我正在复制解决方案(来自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

您不必指定RootElement。这只适用于root不在*的情况。尝试一下,让我知道它是否有效。以下是我们测试该项目的反序列化器的方法:https://github.com/restsharp/RestSharp/blob/master/RestSharp.Tests/XmlDeserializerTests.cs

(EDIT: Updated link to correct file)

(编辑:更新了正确文件的链接)