I have a .net core web application with a XmlDocument, this doesn't return declaration, even without modifications.
I have this code
[HttpPost]
[Consumes("application/xml")]
[Produces("application/xml")]
public ActionResult<XmlDocument> GW1()
{
XmlDocument xmlDocRec = new XmlDocument();
xmlDocRec.Load(Request.Body);
return Ok(xmlDocRec);
}
Request
<?xml version="1.0" encoding="utf-8"?>
<GR User="User1" PropertyCode="90001045">
<GW>1</GW>
</GR>
Response
<GR User="User1" PropertyCode="90001045">
<GW>1</GW>
</GR>
I have this in Startup
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddXmlSerializerFormatters();
I need in response <?xml version="1.0" encoding="utf-8"?> but i don't know why it doesn't not return. In xmlDocRec.InnerXml and xmlDocRec.OuterXml exists.
I haven't class as parameters and response neither, i can't use it for requirement, for that i use Request.Body
Obviously i work with xmlDocRec, i add and update elements, but result is the same. When i work with xmlDocRec, xmlDocRec.InnerXml and xmlDocRec.OuterXml contains <?xml version="1.0" encoding="utf-8" standalone="no"?>. Later, i will need to delete standalone="no" because it must not be in response.
--- Edit
I don't know if it is rigth way, but for now i'm using this
[HttpPost]
public ContentResult GW1()
{
XmlDocument xmlDocRec = new XmlDocument();
xmlDocRec.Load(Request.Body);
return new ContentResult
{
ContentType = "application/xml",
Content = xmlDocRec.OuterXml,
StatusCode = 200
};
}
With that, i don't need Consumes, Produces and AddXmlSerializerFormatters in startup.
If some know a better way I'm willing to try.