472,131 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 software developers and data experts.

Can't get encoding="iso-8859-1"

I do the following:

StringBuilder xml = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.Encoding = Encoding.GetEncoding("iso-8859-1");
xws.Indent = true;
XmlWriter xtw = XmlTextWriter.Create(new StringWriter(xml), xws);
....

But the xml it creates has encoding="utf-16"

Any idea what I'm missing?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
Mar 12 '07 #1
4 10247
David Thielen wrote:
I do the following:

StringBuilder xml = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.Encoding = Encoding.GetEncoding("iso-8859-1");
xws.Indent = true;
XmlWriter xtw = XmlTextWriter.Create(new StringWriter(xml), xws);
...

But the xml it creates has encoding="utf-16"

Any idea what I'm missing?
I think StringWriter always has UTF-16 as its encoding as .NET strings
are internally UTF-16 encoded.
If you really need a string declaring a different encoding then I think
you can do it as follows:

public class StringWriterWithEncoding : StringWriter {
private Encoding myEncoding;
public StringWriterWithEncoding (Encoding encoding) : base () {
myEncoding = encoding;
}
public override Encoding Encoding
{
get
{
return myEncoding;
}
}
}

then create e.g.

StringWriter stringWriter = new
StringWriterWithEncoding(Encoding.GetEncoding("ISO-8859-1"));
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteElementString("root", "Kibo");
xmlWriter.WriteEndDocument();
}
Console.WriteLine(stringWriter.ToString());

that way the result declares e.g. ISO-8859-1.

But encoding does only matter for streams not for strings so the above
is often not necessary, if you write directly to a stream then the
encoding should be taken from XmlWriterSettings.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 12 '07 #2
What I need is to create a byte[] that is the xml file in memory. And the xml
file does need to be iso-8859-1. What is the best way to do that?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm


"Martin Honnen" wrote:
David Thielen wrote:
I do the following:

StringBuilder xml = new StringBuilder();
XmlWriterSettings xws = new XmlWriterSettings();
xws.Encoding = Encoding.GetEncoding("iso-8859-1");
xws.Indent = true;
XmlWriter xtw = XmlTextWriter.Create(new StringWriter(xml), xws);
...

But the xml it creates has encoding="utf-16"

Any idea what I'm missing?

I think StringWriter always has UTF-16 as its encoding as .NET strings
are internally UTF-16 encoded.
If you really need a string declaring a different encoding then I think
you can do it as follows:

public class StringWriterWithEncoding : StringWriter {
private Encoding myEncoding;
public StringWriterWithEncoding (Encoding encoding) : base () {
myEncoding = encoding;
}
public override Encoding Encoding
{
get
{
return myEncoding;
}
}
}

then create e.g.

StringWriter stringWriter = new
StringWriterWithEncoding(Encoding.GetEncoding("ISO-8859-1"));
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
{
xmlWriter.WriteStartDocument();
xmlWriter.WriteElementString("root", "Kibo");
xmlWriter.WriteEndDocument();
}
Console.WriteLine(stringWriter.ToString());

that way the result declares e.g. ISO-8859-1.

But encoding does only matter for streams not for strings so the above
is often not necessary, if you write directly to a stream then the
encoding should be taken from XmlWriterSettings.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 12 '07 #3
Hi Dave,

It seems like XmlTextWriter.Create() method has some issue with
StringBuilder. XmlWriter.Encoding always be set as utf-16.

However, if you just want to create a byte[] array of xml file(iso-8859-1
encoded) in memory. I suggest you may consider using MemoryStream as
below. This method work fine on my machine. I Hope this will help.

XmlWriterSettings xws = new XmlWriterSettings();
xws.Encoding = Encoding.GetEncoding("iso-8859-1");
xws.Indent = true;

MemoryStream ms = new MemoryStream();
XmlWriter xtw = XmlTextWriter.Create(ms, xws) ;

xtw.WriteStartElement("bk", "book", "urn:books");
xtw.WriteAttributeString("genre", "urn:books", "mystery");
xtw.WriteElementString("price", "19.95");
xtw.WriteEndElement();
xtw.Flush();

byte[] msb=ms.GetBuffer();
//String a = Encoding.GetEncoding("iso-8859-1").GetString(msb);

Please try this method and let me know whether this is what you need.
Have a great day.
Sincerely,
Wen Yuan

Mar 13 '07 #4
perfect - thanks
--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm


""WenYuan Wang"" wrote:
Hi Dave,

It seems like XmlTextWriter.Create() method has some issue with
StringBuilder. XmlWriter.Encoding always be set as utf-16.

However, if you just want to create a byte[] array of xml file(iso-8859-1
encoded) in memory. I suggest you may consider using MemoryStream as
below. This method work fine on my machine. I Hope this will help.

XmlWriterSettings xws = new XmlWriterSettings();
xws.Encoding = Encoding.GetEncoding("iso-8859-1");
xws.Indent = true;

MemoryStream ms = new MemoryStream();
XmlWriter xtw = XmlTextWriter.Create(ms, xws) ;

xtw.WriteStartElement("bk", "book", "urn:books");
xtw.WriteAttributeString("genre", "urn:books", "mystery");
xtw.WriteElementString("price", "19.95");
xtw.WriteEndElement();
xtw.Flush();

byte[] msb=ms.GetBuffer();
//String a = Encoding.GetEncoding("iso-8859-1").GetString(msb);

Please try this method and let me know whether this is what you need.
Have a great day.
Sincerely,
Wen Yuan

Mar 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.