C# 从REST响应创建XML文件
我使用以下方法发出rest请求,该请求以xml格式返回数据-C# 从REST响应创建XML文件,c#,xml,C#,Xml,我使用以下方法发出rest请求,该请求以xml格式返回数据- public void Test() { string requestUri = string.Empty; try { requestUri = String.Format("uri/{0}?locales={1}", "test", "en-gb"); HttpClient client = new HttpClient()
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文件?由于内容已经是xml,只需将此.writeFile(content)更改为
File.WriteAllText("foo.xml", content);
另外,
try{}catch(异常e){throw e}
也不好。通过重新检索松散的堆栈信息。只要不做任何事情,抛出的异常就会出现在调用方面前。response.ContentType=“text/xml”;它将在哪个位置创建文件?基本上是在您的exe
/dll
位置。在“工作目录”;默认情况下,从中运行程序。但是您可以使用完全限定的名称,例如@“c:\mydir\foo.xml”。请注意,@“告诉C#编译器不要将\解释为转义。@RichardSchneider非常感谢您。