我正在尝试使用restsharp
将其反序列化为c#对象在标记为重复的问题说明之前,我已经查看了以下堆栈溢出问题,但它没有工作
回应:
<response>
<TAG>
<tagid>1</tagid>
<mac>00:12:8E:12:2F:34</mac>
</TAG>
<TAG>
<tagid>2</tagid>
<mac>00:11:8E:12:3F:11</mac>
</TAG>
</response>
我希望使用的我的课程,但只有它表明count = 0 ...
public class Response
{
public Response()
{
this.Tag = new List<Tag>();
}
[XmlElement()]
public List<Tag> Tag { get; set; }
}
public class Tag
{
public string Tagid { get; set; }
public string Mac { get; set; }
}
尝试反序列化的代码
var request = new RestRequest
{
Resource = "api/notimportant",
RequestFormat = DataFormat.Xml,
Method = Method.GET,
};
request.AddParameter("fields", "all");
var client = new RestClient
{
BaseUrl = new Uri(_url),
};
client.AddHandler("application/xml", new DotNetXmlDeserializer());
var response = client.Execute<Response>(request);
我的很多考试
如果我使用了响应,我什么也得不到,但是当我使用标签时,我只获得了第一个元素,而不是列表,而是我想要的...
答案 0 :(得分:2)
似乎部分区分大小写。所有大写的<TAG>
元素都将其抛弃。当我将类更改为此时,我终于开始工作了:
public class Response
{
public Response()
{
this.Tags = new List<TAG>();
}
public List<TAG> Tags { get; set; }
}
public class TAG
{
public string Tagid { get; set; }
public string Mac { get; set; }
}
如果没有实际的其余API来调用,这就是我测试它所做的:
var d = new XmlDeserializer();
var response = new RestSharp.RestResponse();
response.Content = @"<response>
<TAG>
<tagid>1</tagid>
<mac>00:12:8E:12:2F:34</mac>
</TAG>
<TAG>
<tagid>2</tagid>
<mac>00:11:8E:12:3F:11</mac>
</TAG>
</response>";
var result = d.Deserialize<Response>(response);