如果返回格式是xml,如何删除web api中的模式节点?

我有一个web api方法,它将format作为一个参数,提供返回xml和json。方法返回的数据类型是DataTable.In json格式一切看起来很好但是xml格式的数据表的模式和xml节点中的一些其他属性还返回。如何返回仅包含datatable数据的简单xml?另外,我在WebApiConfig中使用QueryStringMapping。

这是WebApiConfig代码

public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("format", "json", new MediaTypeHeaderValue("application/json"))); config.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("format", "xml", new MediaTypeHeaderValue("application/xml"))); GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; } 

这是控制器方法的伪代码

 [BasicAuthentication] [Route("api/{tablename}")] [HttpGet] public IHttpActionResult Get(string tablename, string orders = "", int limit = 100) { DataTable dt = new DataTable{TableName="resource"}; //... Database connection and getting result return Ok(new Response{ limit = limit,count=dt.Rows.Count, data =dt }); } 

和响应模型

 public class Response { public int limit { get; set; } public int count { get; set; } public DataTable data { get; set; } } 

返回xml的示例

   1 1                    1      

总之,我想只返回数据节点中没有任何属性的资源节点。

在发布此问题后我找到了答案。 问题是数据表对象的XmlSchema。 编写自定义数据表xml序列化程序,并在IXmlSerializable接口的GetSchema中返回null,并覆盖它。 自定义序列化器就在那里。