Tuesday, August 23, 2005

some interesting responses on UTF-16 problem with XMLTextWriter

This is what hb replied with :

yar may be, because
utf-16 is two byte per character.
so with string writer its unicode.
if u enforece ascii then utf-8 would be there.

(note: my reply is just an argument, i might need correction, so dont hesitate to correct me)


I replied hb's mail as following:

yes exactly thats wat the point is.. it sounds reasonable for string writer to generate a utf-16 prologues .. but my amazement revolves around the fact there isnt any support for parametrizing prologue specually encoding specs..

This is what Ahsan had to add:

There is one thing that i think you are missing here. you should be using XmlTextWriter for writing xml to stream.
Dim writer As New System.Xml.XmlTextWriter("", System.Text.Encoding.Unicode)

here you specify the encoding for the xml so it spefically know in which way it should be written.

Stringwriter is generic for all types of files, it will not cater specifics of the xml schema

Also please read this
This tell the capabilities of IE

Follwing is my response to Ahsan's mail:

Ahsan my friend! you dint get me right. System.Xml.XMLTextWriter has three constructors

1- public XmlTextWriter(Stream, Encoding);// IOStream required
2- public XmlTextWriter(TextWriter); // valid reference to TextWriterRequired
3- public XmlTextWriter(string, Encoding); // string is for FileName

The problem is I dont want to user stream (binary or filestream). I want to build an an in memory representation of XML Document without using binary/file stream ( requirement!). Now I am left with using only constrcutor number 2.

And I believe you know that TextWriter is an abstract base class so I am left to look for any of the two implementations 1- StreamWriter and 2- StringWriter. And you can realize i cant go for option 1 so it has to be StringWriter and thats where I faced the problem that i posted. What do you say ?

System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(stringWriter);
xmlTextWriter.WriteStartDocument(); // here it writes prologue :/

1 comment:

Steve Austin said...

Interesting blog. I have a xml db blog.