Groups | Blog | Home
all groups > dotnet xml > april 2006 >

dotnet xml : Howto select encoding ISO-8859-1


Michael H
4/3/2006 2:40:02 AM
I'm would like to enconde my XML into ISO-8859-1, but I can't seem to find
howto. ANy suggestions?

StringWriter writer = new StringWriter();
XmlTextWriter xmlWriter = new XmlTextWriter(writer);
xmlWriter.Formatting = Formatting.Indented;
doc.Save(writer);
return writer.ToString();
Michael H
4/3/2006 5:01:02 AM
[quoted text, click to view]

I would like to set the encoding, as my client has difficulties accepting
the utf-16 in the document-header. Your example requires the use of a file -
Martin Honnen
4/3/2006 1:36:25 PM


[quoted text, click to view]

If you serialize to a .NET string then it does not make much sense to
talk about encodings as .NET strings are sequences of Unicode
characters. So unless you serialize to a stream of bytes encoding does
not matter. If you wanted to serialize to an ISO-8859-1 encoded file
then you could use e.g.

XmlTextWriter xmlWriter = new XmlTextWriter("file.xml",
Encoding.GetEncoding("ISO-8859-1"));
xmlWriter.Formatting = Formatting.Indented;
xmlDocument.Save(xmlWriter);
xmlWriter.Close();

--

Martin Honnen --- MVP XML
Martin Honnen
4/3/2006 3:16:06 PM


[quoted text, click to view]

As said, a .NET string is a sequence of Unicode characters whereas
encoding only matters for byte sequences. How to send your XML to your
client? Do you send that over HTTP? Without knowing how you pass on your
string it is hard to make a suggestion, a local file is of course not
necessary if you simply POST the XML with an HTTP request for instance:

XmlTextWriter xmlWriter = new
XmlTextWriter(httpWebRequestInstance.GetRequestStream(),
Encoding.GetEncoding("ISO-8859-1"));
xmlWriter.Formatting = Formatting.Indented;
xmlDocument.Save(xmlWriter);


--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button