我有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;
}
我希望将该响应更改为XML
答案 0 :(得分:0)
转到Global.asax
,但遵循Application_Start()
中的代码:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseDataContractSerializer = true;
或者转到WebApiConfig.cs
并将此行放在Register(HttpConfiguration config)
中:
config.Formatters.XmlFormatter.MediaTypeMappings.Add(
new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));