I am trying to use XmlWriter to create XML. I need to have the XML Declaration to be
< xml version="1.0" encoding="ISO-8859-1" >. I tried to set the Encoding in the XmlWriterSettings to "ISO-8859-1". I see the Encoding changes to “ISO-8859-1” in the setting but no matter what I do the resulting XML Declaration always looks like < xml version="1.0" encoding="utf-16" >. I don’t know if this is a bug or a feature.
Here is my code:
StringBuilder^ sbXml = gcnew StringBuilder();
XmlWriterSettings^ pXmlSettings = gcnew XmlWriterSettings();
XmlWriter^ w = nullptr;
pXmlSettings->Encoding = Encoding::GetEncoding( 1252 /*"ISO-8859-1"*/ );
pXmlSettings->OmitXmlDeclaration = false;
w = XmlWriter::Create( sbXml, pXmlSettings );
w->WriteStartDocument();
w->WriteStartElement( "root" );
w->WriteEndElement();
w->WriteEndDocument();
w->Flush();
Could anione please tell me how to cretae XML with a declaration of < xml version="1.0" encoding="ISO-8859-1" > using XmlWriter
Thank you.

Reset XML Declaration using XmlWriter
sivanu
You can use MemoryStream and use byte array.
mmc071
Thanks for the idea. It works with MemoryStream. Here is the code, if someone is interested.
MemoryStream^ strmMm = gcnew MemoryStream();
XmlWriterSettings^ pXmlSettings = gcnew XmlWriterSettings();
XmlWriter^ w = nullptr;
pXmlSettings->Encoding = Encoding::GetEncoding( "ISO-8859-1" );
pXmlSettings->OmitXmlDeclaration = false;
w = XmlWriter::Create( strmMm, pXmlSettings );
w->WriteStartDocument();
w->WriteStartElement( "root" );
w->WriteEndElement();
w->WriteEndDocument();
w->Flush();
String^ s = Encoding::ASCII->GetString( strmMm->GetBuffer() );
NimeshPatel
The following solution will also work:-
XmlWriterSettings^ pXmlSettings = gcnew XmlWriterSettings();
XmlWriter^ w = nullptr;
pXmlSettings->Encoding = Encoding::GetEncoding( 1252 /*"ISO-8859-1"*/ );
pXmlSettings->OmitXmlDeclaration = false;w = XmlWriter::Create( sbXml, pXmlSettings );
w->WriteProcessingInstruction( "xml", "version'=1.0' encoding='ISO-8859-1' >" );
w->WriteStartDocument();
w->WriteStartElement( "root" );
w->WriteEndElement();
w->WriteEndDocument();
w->Flush();
Hope it helps. I was also taken by surprise by XmlWriter's behaviour, see my post http://blogs.geekdojo.net/richardhsu/
Regards,
Richard Hsu.