Unable to get exact Encoding specified in xmlWriteSettings



I am trying to get xml of an object. To further streamline the xml i have added some settings like indent= true and Enconding.


I have set the encoding to "ISO-8859-1" but when the code get executed oXmlWriter's setting become utf-16 instead of ISO-8859-1" Do anyone have idea why it is behaving so ?



public static string ObjectToXml(object obj)
{
var oXmlSerializer = new XmlSerializer(obj.GetType());
var oStringWriter = new StringWriter();
var oXmlSerializerNamespaces = new XmlSerializerNamespaces();
oXmlSerializerNamespaces.Add(string.Empty,string.Empty);
var oXmlWriterSettings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = false,
Encoding = Encoding.GetEncoding("ISO-8859-1")
};

var oXmlWriter = XmlWriter.Create(oStringWriter,oXmlWriterSettings);
oXmlSerializer.Serialize(oXmlWriter, obj, oXmlSerializerNamespaces);
oXmlWriter.Close();
return oStringWriter.ToString();
}

No comments:

Post a Comment