Global.asax.cs中的Application_Start
方法添加
GlobalConfiguration.Configuration.Formatters.Clear(); GlobalConfiguration.Configuration.Formatters.Add(new XmlMediaTypeFormatter()); GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
默认就有4个Formatter。不需要添加

.net core写在public void ConfigureServices(IServiceCollection services) 方法中
builder.Services.AddControllers(op => { op.RespectBrowserAcceptHeader = true; }).AddXmlSerializerFormatters();
postman参考:


如果使用Dynamic类型,需要添加xmlInClude来指定序列化的类
cs
await Task.Run(new Action(() =>
{
response.data = _db.Database.SqlQueryRaw<OutputEntityM>(sql).FirstOrDefault();
}));
cs
[XmlInclude(typeof(OutputEntityC))]
[XmlInclude(typeof(OutputEntityM))]
public class ResponseEntity
{
public string flag { get; set; }
public string message { get; set; }
public dynamic data { get; set; }
}