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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Gowtham RajamanickamPosted Apr 7, 2015, 4:30 AM
Pradeep ShetPosted Apr 7, 2015, 4:03 AM
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