2015-08-25 62 views
1

XML文件,我有如下以供製作以XML格式返回數據的其餘請求方法 -從創建REST響應

public void Test() 
    { 
     string requestUri = string.Empty; 

     try 
     { 
      requestUri = String.Format("uri/{0}?locales={1}", "test", "en-gb"); 

      HttpClient client = new HttpClient(); 
      client.BaseAddress = new Uri(requestUri); 

      HttpResponseMessage response = client.GetAsync(requestUri).Result; 
      if (response.IsSuccessStatusCode) 
      { 
       string content = response.Content.ReadAsStringAsync().Result; 
       // Want to create xml file here 
       this.WriteToFile(content); 
      } 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
    } 

如何創建從我從API得到的迴應一個xml文件?

+0

response.ContentType =「text/xml」; –

回答

2

由於內容已經是XML只是改變this.WriteToFile(content)

File.WriteAllText("foo.xml", content); 

另外,try {} catch (Exception e) {throw e}是壞的。重新推出堆疊信息。簡單地不做任何事,拋出的異常會讓你的呼叫者感到不安。

+0

它會在哪個位置創建文件? – Akshay

+0

基本上在你的'exe' /'dll'位置。 –

+0

在「工作目錄」;默認情況下是你從哪裏運行程序。但是您可以使用完全限定的名稱,例如@ 「C:\ MYDIR \ foo.xml」。請注意@「告訴C#編譯器不要將\解釋爲轉義。 –