2012-03-23 12 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哭關於「串」必須是一個非抽象類型與公共參數構造函數。

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

由於克勞斯約根森BTW對活的瓷磚一真棒教程!

我只是想下載的字符串作爲我已經有一個解析器等待它來分析它:-)

回答

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