.Net Core XmlDocument doesn't return declaration

Viewed 1369

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.

2 Answers

You could try to configure your XmlSerializerOutputFormatter in the ConfigureServices method using XmlWriterSettings.OmitXmlDeclaration and setting it to false as this is set to true by default.

private void ConfigureXmlOutpuFormatter(MvcOptions options)
{
     XmlWriterSettings settings = new XmlWriterSettings
     {
       OmitXmlDeclaration = false
     };
     options.OutputFormatters.Add(new XmlSerializerOutputFormatter(settings));
}

You might want to craft your response with explicit property values set on the XmlDeclaration class.

I suggest you take a look at the XmlDeclaration Class

[HttpPost]
[Consumes("application/xml")]
[Produces("application/xml")]
public ActionResult<XmlDocument> EchoXmlAndChangeEncoding()
{
    string requestXML = Request.Body;

    XmlDocument doc = new XmlDocument();
    doc.Load(new StringReader(requestXML));

    // Grab the XML declaration. 
    XmlDeclaration xmldecl = doc.ChildNodes.OfType<XmlDeclaration>().FirstOrDefault();
    xmldecl.Encoding = "UTF-8";
    xmldecl.Standalone = null;   // <-- or do whatever you need

    ... // set other declarations here

    // Output the modified XML document 
    return Ok(doc.OuterXml);
}
Related