質問

WP7プロジェクトのためにRestSharpを試しています。RestSharpを使っていくつかのXMLを逆シリアル化する問題を抱えてください。オブジェクトはnullです。これが関連する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);   

    }
.

誰かが問題に光を和らげることができるならば、事前にありがとう。

役に立ちましたか?

解決

最初のオフでは、閉じるタグは閉じるタグである必要があります。それを修正した後、私は囲まれたクラスを設定しました:

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

オリジナルの従業員クラスを保持します。

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

}
.

その後、それを逆シリアル化するために:

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

他のヒント

request.RootElement = "employee";を追加する既存のコードで動作するはずです。ツリーを離れて始めたくない場合は、階層全体に一致するクラスを作成する必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top