我試用了一個WP7項目的RestSharp。在使用RestSharp反序列化一些XML時遇到一些麻煩。該對象爲空。下面是一些相關的XML的:使用RestSharp反序列化XML
<?xml version="1.0" encoding="utf-8"?>
<api_response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<response_data>
<employee_information>
<employee>
<employee_sf_name>David</employee_sf_name>
<employee_first_name>Dave</employee_first_name>
<employee_last_name>Jones</employee_last_name>
</employee>
</employee_information>
</response_data>
</api_response>
,這裏是我的要求:
public static void executeRequest(Action<string> callback, string method)
{
var client = new RestClient();
var request = new RestRequest(Method.POST);
client.BaseUrl = App.url + method;
request.AddParameter("secret_key", Application.secret_key);
request.AddParameter("email", Application.email);
request.AddParameter("password", Application.password);
client.ExecuteAsync<Employee>(request, response =>
{
callback(response.Content); //prints the response as output
Debug.WriteLine("firstname " + response.Data.employee_first_name);
});
}
而這裏的Employee對象:
public class Employee
{
public Employee() { }
public int employee_id { get; set; }
public String employee_first_name { get; set; }
public String employee_last_name { get; set; }
}
因爲響應回來罰款我試圖反序列化它一個單獨的功能,但沒有成功:
public static void parse(string data)
{
Debug.WriteLine(data);
XmlDeserializer xml = new XmlDeserializer();
Employee employee = new Employee();
employee = xml.Deserialize<Employee>(new RestResponse() { Content = data });
Debug.WriteLine("last name " + employee.employee_last_name);
Debug.WriteLine("firstname " + employee.employee_first_name);
}
如果有人能提供此問題,請提前致謝。
是否存在缺少的XML標題或者您是否只是沒有將它包含在上面? –
@MattLacey它只是沒有包括在內。我編輯了我的問題並添加了它。 – joe