2012-03-23 44 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; 
     } 
    } 
}