[英]RestSharp's deserialization doesn't work, C# Serialization does
I've got some troubles with RestSharp's builtin deserialization. RestSharp的内置反序列化让我有些麻烦。
My Code: 我的代码:
RestClient client = new RestClient("https://example.com/rest/");
client.ClearHandlers();
client.AddHandler("application/xml", new RestSharp.Deserializers.XmlDeserializer());
var request = new RestRequest();
request.Resource = "member/findScoutIdsForOrganization/{organizationId}";
request.AddUrlSegment("organizationId", orgId);
var response = client.Execute<wsScoutIdList>(request);
var scoutids = response.Data;
XmlSerializer serializer = new XmlSerializer(typeof(wsScoutIdList));
var result = serializer.Deserialize(XmlReader.Create(new StringReader(response.Content)));
wsScoutIdList.java: (generated by xsd2code++) wsScoutIdList.java :(由xsd2code ++生成)
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlRootAttribute("ScoutIdList", Namespace="", IsNullable=false)]
public partial class wsScoutIdList
{
private List<string> _list;
public wsScoutIdList()
{
this._list = new List<string>();
}
[System.Xml.Serialization.XmlElementAttribute("list", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
public List<string> list
{
get
{
return this._list;
}
set
{
this._list = value;
}
}
}
xml: xml:
<?xml version ="1.0" encoding="UTF-8" standalone="yes" ?>
<ScoutIdList>
<list>id1</list>
<list>id2</list>
<list>id3</list>
</ScoutIdList>
System.XML.Serialization works, but RestSharp doesn't. System.XML.Serialization有效,但RestSharp无效。 The list is simply empty.
该列表是空的。
I also tried json, not working either. 我也尝试了json,也没有用。
There are few things: 有几件事:
You don't need this: 您不需要这个:
client.AddHandler("application/xml", new RestSharp.Deserializers.XmlDeserializer());
Instead use this: 而是使用此:
request.RequestFormat = DataFormat.Xml;
Also you if a parameter is not in format of parameterName=parameterValue you can't use this: 另外,如果参数的格式不是parameterName = parameterValue,则不能使用此参数:
request.AddUrlSegment("organizationId", orgId); request.AddUrlSegment(“ organizationId”,orgId);
Instead just do this: 而是这样做:
request.Resource = "member/findScoutIdsForOrganization/ + orgId";
If you want to use default serializer you just can remove this: 如果要使用默认的序列化程序,则可以删除以下内容:
XmlSerializer serializer = new XmlSerializer(typeof(wsScoutIdList));
So your code should look like this: 因此,您的代码应如下所示:
RestClient client = new RestClient("https://example.com/rest/");
request.RequestFormat = DataFormat.Xml;
var request = new RestRequest();
request.Resource = "member/findScoutIdsForOrganization/" + orgId;
var response = client.Execute<wsScoutIdList>(request);
var scoutids = response.Data;
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.