问题
I have a web api method that takes format as a parameter that provides returning both xml and json.The data type that method return is DataTable.In json format everything looks fine but in xml format the schema of datatable and some other attributes in xml nodes also returning.How to return simple xml that includes only data of datatable?Also, I am using QueryStringMapping in WebApiConfig.
This is WebApiConfig Code
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;
}
This is a pseudo code of controller method
[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 });
}
and the Response Model
public class Response
{
public int limit { get; set; }
public int count { get; set; }
public DataTable data { get; set; }
}
The example of returned xml
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<limit>1</limit>
<count>1</count>
<data>
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="resource" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="resource">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:long" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<DocumentElement>
<resource diffgr:id="resource1" msdata:rowOrder="0">
<ID>1</ID>
</resource>
</DocumentElement>
</diffgr:diffgram>
</data>
</Response>
To sum up, I want to return only resource nodes in data node without any attribute.
回答1:
I have found the answer after posting this question. The problem is XmlSchema of datatable object. Writing custom datatable xml serializer with returning null in GetSchema of IXmlSerializable interface with overriding it. The custom serializer is there.
来源:https://stackoverflow.com/questions/39573443/how-to-remove-schema-node-in-web-api-if-return-format-is-xml