2015-11-26 84 views
0

我在RestSharp的內置反序列化中遇到了一些麻煩。RestSharp的反序列化不起作用,C#序列化做

我的代碼:

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:(由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 version ="1.0" encoding="UTF-8" standalone="yes" ?> 
<ScoutIdList> 
    <list>id1</list> 
    <list>id2</list> 
    <list>id3</list> 
</ScoutIdList> 

的System.Xml.Serialization作品,但沒有按RestSharp」噸。該列表簡直是空的。

我也試過json,不工作。

回答

0

有幾件事情:

你不需要這樣的:

client.AddHandler("application/xml", new RestSharp.Deserializers.XmlDeserializer()); 

而是使用此:

request.RequestFormat = DataFormat.Xml; 

另外你如果參數不是在參數名稱=的格式parameterValue你不能使用這個:

request.AddUrlSegment(「organizationId」,orgId);

相反只是這樣做:如果你想使用默認的序列,你纔可以刪除

request.Resource = "member/findScoutIdsForOrganization/ + orgId"; 

這樣的:

XmlSerializer serializer = new XmlSerializer(typeof(wsScoutIdList)); 

所以,你的代碼應該是這樣的:

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; 

希望它有幫助

+0

'reque st.RequestFormat = DataFormat.Xml;'不起作用,它使用json然後 'request.AddUrlSegment(「organizationId」,orgId);'是推薦的方法,這沒有什麼錯。 額外的'XmlSerializer'只是爲了表明這個工作正常。 RestSharper方法已經在那裏,並沒有工作。 – filnko