从API响应创建xml

本文关键字:xml 创建 响应 API | 更新日期: 2023-09-27 18:01:34

我有以下应用程序,用于发出GET请求,以JSON格式返回数据:

class Program
{
    static void Main(string[] args)
    {
        RunAsync().Wait();
    }
    static async Task RunAsync()
    {
        using (var client = new HttpClient())
        {
            //Send HTTP request
            client.BaseAddress = new Uri("httpMyURI");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await client.GetAsync("api/people/.....");
            if (response.IsSuccessStatusCode)
            {
                var person = response.Content.ReadAsAsync<IEnumerable<Persons>>().Result;
                foreach (var i in person)
                {
                    Console.WriteLine("{0}'t{1}-{2}", i.FirstName, i.LastName, i.Peopleid); 
                }
            }
        }
    }
}
class Persons
{
    public string Peopleid { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

我如何从我得到的结果创建一个xml文件?我想我首先要转换JSON到xml然后创建一个xml文件?

从API响应创建xml

使用Newtonsoft Json可以很容易地做到这一点。网络图书馆。查看操作中的示例:http://www.newtonsoft.com/json/help/html/convertingjsonandxml.htm

后面的行是

XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);