1)How to send request to web api using xml format in visual studio 2013.
-->how to get response from web api in the xml format.
2)How to convert xml files to class.
3)How to connect to web api.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pradeep ShetRank #595 · 4K reputationPosted Apr 7, 2015, 12:03 AM EDT
1)To get the output of webapi in xml, you only need to set the header while calling API method from client
Accept: 'application/xml'
If you want your webapi sohuld emit only xml then set the line in global.asax file
GlobalConfiguration.Configuration.Formatters.XmlFormatter.MediaTypeMappings.Add(new QueryStringMapping("xml", "true", "application/xml"));
Hope this works.
2)To convert xml to a class, you need to use xmlSerializer class
var deserializer = new XmlSerializer(className.GetType());
var convertedXml = deserializer.Deserialize(textReader);
textReader.Close();
3)To connect to webapi from jquery client is simple directly call the api url in ajax call.
but to connect on serverside coding, then create a object of HttpClient
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost/");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/xml"));
HttpResponseMessage response = client.GetAsync("api/User").Result;
if (response.IsSuccessStatusCode)
{
var users = response.Content.ReadAsAsync&
}
If this answers your question plz mark it as answered
Gowtham RajamanickamRank #57 · 29K reputationPosted Apr 7, 2015, 12:30 AM EDT