Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从REST响应创建XML文件_C#_Xml - Fatal编程技术网

C# 从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()

我使用以下方法发出rest请求,该请求以xml格式返回数据-

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非常感谢您。