Return Xml from Azure Function

To return XML from a function, you can use ContentResult instead of ActionResult. Relevant statement: return new ContentResult { Content = retval.InnerXml, ContentType = “application/xml” };

using System.Threading.Tasks;
using System.Xml;

namespace Function.RelatieHelper
{
    public static class VictimMessage
    {
        [FunctionName("CreateVictimSiblingMessage")]
        public static async Task<IActionResult> CreateVictimSiblingMessage(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string msg = await new StreamReader(req.Body).ReadToEndAsync();
            string victimId = GetHeaderValue(req, "victimId");


            if (msg == null)
            {
                return (ActionResult)new BadRequestObjectResult("XmlMessage ism null");
            }

            if (String.IsNullOrEmpty(victimId))
            {
                return (ActionResult)new BadRequestObjectResult("VictimId is null");
            }

            XmlDocument retval = new XmlDocument();

            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream);
                writer.Write(msg);
                writer.Flush();
                stream.Position = 0;

                retval.Load(stream);

                retval.SelectSingleNode("//*[local-name() = 'relationid']").InnerText = victimId;
                retval.SelectSingleNode("//*[local-name() = 'relation_extern_id']").InnerText = victimId;
                
            }

            return new ContentResult { Content = retval.InnerXml, ContentType = "application/xml" };
                            
        }

        private static string GetHeaderValue(HttpRequest req, string headerName)
        {

            StringValues headerValues;
            string headerValue = null;

            if (req.Headers.TryGetValue(headerName, out headerValues))
            {
                headerValue = headerValues[0];
            }

            return headerValue;

        }
    }
}

XMLDeserialize: Error in XML document (1,2)

Generated service contract and data contracts from a WSDL using svcutil. Next, I tried to deserialize a XML message into the generated object. I received the following error: There is an error in XML document (1,2). After debugging code, I found out the [XmlRoot] attribute was not generated. Below, you see the generated datacontract with the XmlRootAttribute manually added:

[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.Xml”, “4.6.1087.0”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“code”)]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = “http://dsplatform.nl/participant/TechnischGereed&#8221;)]
[System.Xml.Serialization.XmlRootAttribute(“TechnischGereedbericht”, Namespace = “http://dsplatform.nl/participant/TechnischGereed&#8221;, IsNullable = false)]
public partial class TechnischGereedberichtXOPType : BerichtTypeXOP

And then, as an example, the code to deserialize the xml message:

public HttpResponseMessage PostTG([FromBody]string document, string integrationId, string formCode)
{

TechnischGereedberichtXOPType TGbericht;

XmlSerializer serializer = new XmlSerializer(typeof(TechnischGereedberichtXOPType));
using (XmlReader reader = XmlReader.Create(new StringReader(document)))
{
TGbericht = (TechnischGereedberichtXOPType)serializer.Deserialize(reader);
}