2012-03-23 4 views
0

私は有効なXMLを含むURLを持っていますが、RestClientでこれをどのように取得できるかは不明です。私は文字列をダウンロードして、WebClientを持っているのと同じように解析できると思った。RestClientでXMLをダウンロードするには?

行う:

 public static Task<String> GetLatestForecast(string url) 
     { 
      var client = new RestClient(url); 
      var request = new RestRequest(); 

      return client.ExecuteTask<String>(request); 
     } 

はVSは「string」が公共のパラメータなしのコンストラクタを持つ非抽象型でなければならないことについて泣くします。

参照くださいexecutetask:ところでライブタイル上の素晴らしいチュートリアルのクラウス・ヨルゲンセンへ

namespace RestSharp 
{ 
    public static class RestSharpEx 
    { 
     public static Task<T> ExecuteTask<T>(this RestClient client, RestRequest request) 
      where T : new() 
     { 
      var tcs = new TaskCompletionSource<T>(TaskCreationOptions.AttachedToParent); 

      client.ExecuteAsync<T>(request, (handle, response) => 
      { 
       if (response.Data != null) 
        tcs.TrySetResult(response.Data); 
       else 
        tcs.TrySetException(response.ErrorException); 
      }); 

      return tcs.Task; 
     } 
    } 
} 

ありがとう!

私はちょうど私はすでにあなたが望むすべてが文字列であれば、それは:-)

答えて

1

それを解析するのを待っているパーサを持っているとしてだけではなく、このアプローチを使用し、文字列をダウンロードしたい:

namespace RestSharp 
{ 
    public static class RestSharpEx 
    { 
     public static Task<string> ExecuteTask(this RestClient client, RestRequest request) 
     { 
      var tcs = new TaskCompletionSource<string>(TaskCreationOptions.AttachedToParent); 

      client.ExecuteAsync(request, response => 
      { 
       if (response.ErrorException != null) 
        tcs.TrySetException(response.ErrorException); 
       else 
        tcs.TrySetResult(response.Content); 
      }); 

      return tcs.Task; 
     } 
    } 
} 
関連する問題