6

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.

Imran Sh
  • 1,623
  • 4
  • 27
  • 50
mr.
  • 679
  • 12
  • 30
  • use attributes like in the samples in this question http://stackoverflow.com/questions/12590801/remove-namespace-in-xml-from-asp-net-web-api – Thorarins Sep 26 '16 at 07:31
  • There is something strange that formatter about json is working such as Intended but formatting about xml such as config.Formatters.XmlFormatter.UseXmlSerializer = true; not working in WebApiConfig – mr. Sep 26 '16 at 07:41
  • 1
    json doesnt have the same namespace and schema information that xml allows thats why it works better with json – Thorarins Sep 26 '16 at 08:18
  • 1
    maybe you need to write a new xmlformatter http://stackoverflow.com/questions/17327677/xml-namespaces-in-asp-net-web-api – Thorarins Sep 26 '16 at 08:25
  • @Thorarins you are right. The answer in this question solved my problem. I havent seen this answer before asking. The problem is all about Datatable object. http://stackoverflow.com/questions/14571927/net-webapi-datatable. – mr. Sep 26 '16 at 10:04
  • yes, datatable has its flaws – Thorarins Sep 26 '16 at 11:02
  • Do you need to deserialize the XML as a `DataTable` later? Or are you only interested in serialization? – dbc Sep 26 '16 at 16:24
  • Also, do you care about the wrapper `DocumentElement` node? Can you specify the exact XML required? – dbc Sep 26 '16 at 17:18
  • @dbc no i dont need to deserialize the xml.The api method taking a tablename and returing its count,its data and given limit number of data.I am trying to build a generic method for each table. – mr. Sep 27 '16 at 06:42
  • @mayk did you check kamyonlar before serialization – Mustafa ASAN Sep 27 '16 at 11:04

1 Answers1

2

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.

Community
  • 1
  • 1
mr.
  • 679
  • 12
  • 30