Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read XML from ASP.NET Web API?

I have a Web API that would read XML and pass it to the appropriate model for processing.

How can I receive that XML that is coming in? Which datatype should I use?

Do I use StreamReader, StreamContent or XmlDocument or other?

like image 489
NotMe Avatar asked Sep 04 '13 07:09

NotMe


People also ask

Can ASP Net Web API return XML?

The . NET Framework WebAPI 2 by default will serialize responses from a controller method using the built in serializers. Out the box it supports JSON and XML.

How does Web API send XML data?

If you want to send XML data to the server, set the Request Header correctly to be read by the sever as XML. xmlhttp. setRequestHeader('Content-Type', 'text/xml'); Use the send() method to send the request, along with any XML data.


1 Answers

Any incoming content can be read as a stream of bytes and then processed as required.

public async Task<HttpResponseMessage> Get() {

   var stream = await Request.Content.ReadAsStreamAsync();

   var xmlDocument = new XmlDocument();
   xmlDocument.Load(stream);

   // Process XML document

   return new HttpResponseMessage();
}
like image 185
Darrel Miller Avatar answered Oct 05 '22 10:10

Darrel Miller