Add <?xml version="1.0" encoding="UTF-8" standalone="yes"?> to my xml response

  • Thread starter Thread starter SegunOk
  • Start date Start date
S

SegunOk

Guest
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!

Continue reading...
 

Similar threads

Back
Top