Domanda

Sto provando a restsharp per un progetto WP7.Avere alcuni problemi a deseriabilizzare un po 'di XML con RestSharp.L'oggetto è nullo.Ecco alcuni dei pertinenti 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>
.

Ed Ecco la mia richiesta:

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);
        });
    }
.

Ed ecco l'oggetto dipendente:

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; }

}
.

Poiché la risposta torna indietro bene ho provato a deerializzazione in una funzione separata, ma senza successo:

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);   

    }
.

Grazie in anticipo se qualcuno può far luce sul problema.

È stato utile?

Soluzione

Prima di tutto, il tag di chiusura deve essere un tag di chiusura.Dopo aver risolto questo, ho impostato una classe di allevamento:

public class employee_information
{
    public Employee employee { get; set; }
}
.

Quindi ha mantenuto la tua classe originale dei dipendenti:

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; }

}
.

Allora, per deserializzarlo:

var empInfo = xml.Deserialize<employee_information>((new RestResponse() {Content = data}));
.

Altri suggerimenti

L'aggiunta di request.RootElement = "employee"; dovrebbe funzionare con il tuo codice esistente.Se non vuoi iniziare così lontano dall'albero, devi creare classi che corrispondono all'intera gerarchia.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top