How to display XML body element value C#?
I am working on a project where I am hosting a C# web API on .Net Framework. When I do a post to that endpoint with an XML payload I would like to return one of the elements inside the payload as a response to the post.
Here is the payload:
POST /api/DD_Reg HTTP/1.1 Host: localhost:123 Content-Type: application/xml <?xml version="1.0" ?> <Message> <ProductId>10</ProductId> </Message>
Then on the Endpoint, the c# code is this:
public string Post([FromBody] string inXML) { XmlDocument doc = new XmlDocument(); doc.LoadXml(inXML); XmlNode node = doc.DocumentElement.SelectSingleNode("ProductId"); return node.ToString(); }
And I get an error saying:
{ "Message": "An error has occurred.", "ExceptionMessage": "Value cannot be null.\r\nParameter name: s", "ExceptionType": "System.ArgumentNullException", "StackTrace": at System.IO.StringReader..ctor(String s)\r\n at System.Xml.XmlDocument.LoadXml(String xml)\r\n at CollectNet.Controllers.DD_RegController.Post(String inXML) in \\"file location"\\Controllers\\DD_RegController.cs:line 23\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()" }
I have tried many things and been struggling with this for hours. Seems like no matter what I do the C# side does not see the XML body. The end result should be that the endpoint should display in this example the number 10.
Please help
….Adding to this post, here is the Postman snapshot of sending the data Postman Example Of Post
After updating the question the issue is not in xml, but the parameter is always null.
I have fixed this locally by the following steeps :
1 – I have added in App_Start WebApiConfig
:
config.Formatters.XmlFormatter.UseXmlSerializer = true;
To use xmlSerializer
, because the Web Api uses DataContractSerializer
by default.
2 – Adding class Message
:
public class Message { public string ProductId { get; set; } }
3 – Post method :
public string Post([FromBody]Message message) { return message?.ProductId; }
Result
10
Note that, Verify if Content-Type => application/json is unchecked in Postman, if is already checked.
I hope you find this helpful.