Search code examples
c#xmlasp.net-core-webapicxml-commercexml

How to return xml from Web Api


I'm trying to return the following xml from a web api

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.014/cXML.dtd">
<cXML payloadID="payloadID" xml:lang="en" timestamp="2019-12-12T12:57:27-08:00">
  <Response>
    <Status code="200" text="OK" />
  </Response>
</cXML>

but the actual return is

<cXML payloadID="payloadID" xml:lang="en" timestamp="2019-12-12T12:57:27-08:00">
<Response>
<Status code="200" text="OK" />
</Response>
</cXML>

here's a code snippet

[HttpPost("api/[controller]/{format}"), FormatFilter]
    public IActionResult Post([FromBody] AribaPurchaseOrder.Order Order)
    {
        // Process Order .......

        //Ariba order response
        var response = "";
        response += @"<?xml version=""1.0"" encoding=""UTF-8""?>";
        response += @"<!DOCTYPE cXML SYSTEM ""http://xml.cxml.org/schemas/cXML/1.2.014/cXML.dtd"">";

        response += $@"<cXML payloadID=""{Order.PayloadID}"" xml:lang=""en"" timestamp=""{GetAribaFormatTimeStamp()}"">";
        response += @"<Response>";
        response += @"<Status code=""200"" text=""OK""/>";
        response += @"</Response>";
        response += @"</cXML>";

        return new ObjectResult(response) { StatusCode = 200 };
    }

Can anyone advise how to send the response in the correct format?


Solution

  • return this.Content(response, "text/xml");