473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 10335
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marcus | last post by:
I'm running into a situation that has me adding a value of "Unknown" to a reference table. I am being pulled between two trains of thought, and was curious to get other's input on in. I give an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.