إلغاء تسلسل XML باستخدام RestSharp
-
12-12-2019 - |
سؤال
أحاول استخدام RestSharp لمشروع WP7.تواجه بعض المشاكل في إلغاء تسلسل بعض ملفات 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);
});
}
وهنا كائن الموظف:
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);
}
شكرًا مقدمًا إذا كان بإمكان أي شخص إلقاء بعض الضوء على هذه القضية.
المحلول
أولا، يجب أن تكون علامة الإغلاق علامة إغلاق.بعد إصلاح ذلك، قمت بإعداد فئة مغلفة:
giveacodicetagpre.ثم أبقى فئة الموظف الأصلية:
giveacodicetagpre.إذن، لتحملها:
giveacodicetagpre.نصائح أخرى
إضافة request.RootElement = "employee";
يجب أن تعمل مع التعليمات البرمجية الموجودة لديك.إذا كنت لا تريد أن تبدأ من أسفل الشجرة، فأنت بحاجة إلى إنشاء فئات تتوافق مع التسلسل الهرمي بأكمله.
لا تنتمي إلى StackOverflow