How to remove schema node in web api if return format is xml?
Asked Answered
P

1

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.

Portiere answered 19/9, 2016 at 12:39 Comment(10)
use attributes like in the samples in this question #12591301Calumet
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 WebApiConfigPortiere
json doesnt have the same namespace and schema information that xml allows thats why it works better with jsonCalumet
maybe you need to write a new xmlformatter #17328177Calumet
@Calumet 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. https://mcmap.net/q/1681909/-net-webapi-datatable.Portiere
yes, datatable has its flawsCalumet
Do you need to deserialize the XML as a DataTable later? Or are you only interested in serialization?Bimonthly
Also, do you care about the wrapper DocumentElement node? Can you specify the exact XML required?Bimonthly
@Bimonthly 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.Portiere
@mayk did you check kamyonlar before serializationPulcheria
P
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.

Portiere answered 29/9, 2016 at 8:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.