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?
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.
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With