如何在C#中将JSON响应转换为XML?

如何在C#中将JSON响应转换为XML?,c#,asp.net,json,xml,C#,Asp.net,Json,Xml,我有一个JSON格式的API响应,如何将其转换为c格式的XML响应# 我不知道将JSON转换为XML的脚本的位置 public static async Task<List<PointMaster>> ExecuteTest(string query) { string connStrResult = ConfigurationManager.ConnectionStrings["PostGresConnection"].Conne

我有一个JSON格式的API响应,如何将其转换为c格式的XML响应#

我不知道将JSON转换为XML的脚本的位置

public static async Task<List<PointMaster>> ExecuteTest(string query)
        {
            string connStrResult = ConfigurationManager.ConnectionStrings["PostGresConnection"].ConnectionString;
            NpgsqlConnection connection;
            NpgsqlCommand command;
            NpgsqlDataReader reader;

            List<PointMaster> master = new List<PointMaster>();
            connection = new NpgsqlConnection(connStrResult);
            connection.Open();
            command = new NpgsqlCommand(query, connection);
            reader = command.ExecuteReader();

            while (await reader.ReadAsync())
            {
                PointMaster point = new PointMaster
                {
                    point_id        = Convert.ToString(reader["point_id"]),
                    point_type      = (string)reader["point_type"],
                    sp_geometry     = (PostgisPoint)(reader["sp_geometry"]),
                    msid            = Convert.ToInt32(reader["msid"]),

                };

                master.Add(point);
            }

            return master;
        }
公共静态异步任务执行测试(字符串查询)
{
string connStrResult=ConfigurationManager.ConnectionString[“PostGresConnection”].ConnectionString;
NPGSQL连接;
npgsql命令;
NpgsqlDataReader;
列表主机=新列表();
连接=新的NpgsqlConnection(connStrResult);
connection.Open();
command=newnpgsqlcommand(查询、连接);
reader=command.ExecuteReader();
while(等待reader.ReadAsync())
{
PointMaster点=新的PointMaster点
{
point\u id=Convert.ToString(读卡器[“point\u id]”),
点类型=(字符串)读取器[“点类型”],
sp_geometry=(PostgisPoint)(读取器[“sp_geometry”]),
msid=Convert.ToInt32(读卡器[“msid”]),
};
主。添加(点);
}
返回主机;
}

我希望将该响应更改为XML,转到
Global.asax
,但在
Application\u Start()中执行以下代码:

或者转到
WebApiConfig.cs
并将此行放入
寄存器(HttpConfiguration config)


看看这个问题以及它所链接的问题。我想你会找到你想要的。这回答了你的问题吗?注意:这不会将JSON转换为XML,而是生成XML而不是JSON。从来没有任何JSON,这是实现OP真正想要的东西的正确方法,而不是他们所说的。
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseDataContractSerializer = true;
config.Formatters.XmlFormatter.MediaTypeMappings.Add(  
        new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));