Share via


Add to my xml response

Question

Friday, August 25, 2017 12:31 PM

I've been battling with this old problem and can't get it right. I want my xml output to have 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> in the header. I keep having <?xml version="1.0"?> only. I have stripped the formatters by adding the following codes to WebApiConfig.cs

            config.Formatters.Clear();
            config.Formatters.Add(new IgnoreNamespacesXmlMediaTypeFormatter());
            config.Formatters.XmlFormatter.UseXmlSerializer = true;
            config.Formatters.XmlFormatter.WriterSettings.OmitXmlDeclaration = false;
            config.Formatters.XmlFormatter.WriterSettings.Encoding = System.Text.Encoding.UTF8;

and I have inserted .WriteStartDocument(true) in my XMLHelper -

            

        public static string Serialize(object objectInstance)
        {
            string txt = "";

            var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
            var serializer = new XmlSerializer(objectInstance.GetType());
            var settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = false;
            //settings.Encoding = new UTF8Encoding(false);
            settings.ConformanceLevel = ConformanceLevel.Document;

            var memoryStream = new MemoryStream();
            using (var writer = XmlWriter.Create(memoryStream, settings))
            {
                writer.WriteStartDocument(true);
                serializer.Serialize(writer, objectInstance, emptyNamepsaces);
                txt = Encoding.UTF8.GetString(memoryStream.ToArray());
            }
            return txt;
        }

Please help!

All replies (2)

Friday, August 25, 2017 12:43 PM

XmlDocument xml = new XmlDocument();
XmlNode docNode = xml.CreateXmlDeclaration("1.0", "UTF-8\" standalone=\"yes", "");
xml.AppendChild(docNode);
Console.WriteLine("Header:" + xml.OuterXml);

Try it. (Please don't forget make as answer if that it the answer you want.)


Tuesday, September 3, 2019 8:55 AM

WebApiConfig:config.Formatters.Add(new CustomXmlFormatter() {UseXmlSerializer = true});
config.Formatters.Remove(config.Formatters.XmlFormatter);

CustomXmlFormatter:

public class CustomXmlFormatter : XmlMediaTypeFormatter

    {          public override Task WriteToStreamAsync(Type type,                                                 object value,                                                 Stream writeStream,                                                 HttpContent content,                                                 TransportContext transportContext)         {             try             {                 var xns = new XmlSerializerNamespaces();                 xns.Add(string.Empty, string.Empty);                 var task = Task.Factory.StartNew(() =>                 {                     var serializer = new XmlSerializer(type);                     var writer = XmlWriter.Create(writeStream,                                                   new XmlWriterSettings                                                   {                                                       OmitXmlDeclaration = false, 

Encoding = Encoding.UTF8,                                                       Indent = true                                                   });                     writer.WriteStartDocument(true);                     serializer.Serialize(writer, value, xns);                 });                 return task;             }             catch (Exception)             {                 return base.WriteToStreamAsync(type, value, writeStream, content, transportContext);             }         }     }

   good job!!