[英]How to convert JSON response to XML in C#?
我有一個 JSON 格式的 API 響應,如何將它轉換為 XML 響應 Z240AA2CEC2029C57EZADCEEEE
我不知道將 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
Go 到Global.asax
和Application_Start()
中的以下代碼:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseDataContractSerializer = true;
或 go 到WebApiConfig.cs
並將這一行放入Register(HttpConfiguration config)
:
config.Formatters.XmlFormatter.MediaTypeMappings.Add(
new QueryStringMapping("type", "xml", new MediaTypeHeaderValue("application/xml")));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.