1

The Web API is not returning XML instead just returns JSON

    [HttpGet]
    public IHttpActionResult Get(string Account)
    {
     // other code fo connecting to the SQL seerver and calling the stored procedure
     reader = command.ExecuteReader(); 
     List<QueryResult>qresults = new List<QueryResult>();
       while (reader.Read())
       {
           QueryResult qr = new QueryResult();
           qr.AccountID = reader["AccountID"].ToString();
           qr.CounterSeq = reader["CounterSeq"].ToString();
           qresults.Add(qr);
       }
       DbConnection.Close();
       return Ok(qresults);

When I tried to execute the API in the fiddler client
enter image description here

When I click on JSON it shows like [{"AccountID":"Research","CounterSeq":"3"}]

I have two questions

  1. How can I return the response in the file name QueryResult.xml.

  2. How can we automatically return the XML and not in JSON. I am not sure where to Accept the content does that happen in the Web API application.

I am new to API's not sure where I am missing things. Any help is greatly appreciated

trx
  • 1,847
  • 7
  • 33
  • 71
  • One of your answers is here for #2: http://stackoverflow.com/questions/9956052/how-to-ask-webapi-for-a-specific-content-type I just tested it and it returns XML. For #1 you probably need to investigate how to return a file, but you'll need to create the XML object first probably. – KSib Dec 21 '16 at 20:48

4 Answers4

2

The format of the result will depend on 2 things.

  1. What is allowed
  2. What was requested

Allowed Serializers

What is allowed depends on the included serializers in the WebApiConfig.cs. By default both the xml serializer and json serializer are included. If you only ever wanted to return XML for the whole web application then remove the json formatter like so.

GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.JsonFormatter);

Client Request

The client can specify what they want based on the http header content-type with a value of application/json or application/xml depending on what format they want the data returned.

If the client does not specify anyhting in the header in the request then the first allowed serializer in the list is used as the default.

Igor
  • 55,253
  • 10
  • 80
  • 149
1

Maybe use a DataSet - something like this...

DataSet ds = null;
using (SqlConnection connection = new SqlConnection(sConnectionString))
{
    connection.Open();
    SqlCommand Sqlcommand = new SqlCommand();
    Sqlcommand.CommandType = CommandType.StoredProcedure;
    if (SqlParamlist != null)
    {
        foreach (SqlParameter Sqlparameter in SqlParamlist)
        {
            Sqlcommand.Parameters.AddWithValue(Sqlparameter.ParameterName, Sqlparameter.Value);                                
        }
    }
    Sqlcommand.CommandText = sProc;
    Sqlcommand.Connection = connection;
    sProcParams = sProc + " " + sProcParams;
    using (SqlDataAdapter da = new SqlDataAdapter(Sqlcommand))
    {
        ds = new DataSet("DATATABLE");
        da.Fill(ds, "DATAROW");
        Sqlcommand.Parameters.Clear();
        da.Dispose();
    }
    connection.Close();
}
XmlDataDocument xDoc = new XmlDataDocument();
xDoc.LoadXml(ds.GetXml());
Vanquished Wombat
  • 6,839
  • 4
  • 22
  • 52
1

ASP.NET MVC (API) has the so called media formatters used for data serialization. By default the API is set (from MVC API v2 I think) to be JSON. You can easily set the default to be XML using the global configuration object:

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;

A good introduction can be found here: JSON and XML Serialization in ASP.NET Web API or here: Formatting Response Data. Media formatters are a nice thing, because you can create your own custom formatters and use them in your API

In case you don't find the global configuration, have a look at this SO post.

Community
  • 1
  • 1
keenthinker
  • 7,185
  • 2
  • 33
  • 43
0

How can I return the response in the file name QueryResult.xml.

Do you mean saving the response in a xml file? Well, anyway I suppose you know how to do that, the real issue is the second question, I assume.

How can we automatically return the XML and not in JSON. I am not sure where to Accept the content does that happen in the Web API application.

In your client, just set it to application/xml and then your API should returns the content in xml format.

Excuse me if I did skip something, I couldn't see your screenshot, firewall in job.

P.S: I reply as an answer and not a simple comment just because I have not enough reputation to make a simple comment.

A. J.
  • 60
  • 7