D
Dan
I'm having a problem with a codepiece which works fine under windows (xp)
but has a strange behaviour when run with the CF: I use a XmlTextWriter
which writes into a memory stream using the ANSI encoding (1252); when I
have finished writing, I want to retrieve the text written as a (Unicode)
string. The sample code I paste here outputs a string with some garbage into
it (e.g. < becomes <<, and so forth). Could anyone please explain what I'm
doing wrong? Thanks!!!
--- sample code ---
// prepare memory stream & ANSI XML text writer
MemoryStream st = new MemoryStream();
Encoding ecAnsi = Encoding.GetEncoding(1252);
XmlTextWriter wr = new XmlTextWriter(st, ecAnsi);
wr.Formatting = Formatting.Indented;
// write some dummy XML
wr.WriteStartElement("root");
wr.WriteElementString("item", "I'm an item");
wr.WriteEndElement();
wr.Flush();
// get an array of char's from memory stream: HERE COMES THE GARBAGE...
char[] a = new char[ecAnsi.GetCharCount(st.ToArray(), 0, (int)st.Length)];
ecAnsi.GetDecoder().GetChars(st.ToArray(), 0, (int)st.Length, a, 0);
// create a string from the char array
StringBuilder sb = new StringBuilder(a.Length);
foreach (char c in a) sb.Append(c);
Console.WriteLine(sb.ToString());
wr.Close();
--- end of sample code ---
but has a strange behaviour when run with the CF: I use a XmlTextWriter
which writes into a memory stream using the ANSI encoding (1252); when I
have finished writing, I want to retrieve the text written as a (Unicode)
string. The sample code I paste here outputs a string with some garbage into
it (e.g. < becomes <<, and so forth). Could anyone please explain what I'm
doing wrong? Thanks!!!
--- sample code ---
// prepare memory stream & ANSI XML text writer
MemoryStream st = new MemoryStream();
Encoding ecAnsi = Encoding.GetEncoding(1252);
XmlTextWriter wr = new XmlTextWriter(st, ecAnsi);
wr.Formatting = Formatting.Indented;
// write some dummy XML
wr.WriteStartElement("root");
wr.WriteElementString("item", "I'm an item");
wr.WriteEndElement();
wr.Flush();
// get an array of char's from memory stream: HERE COMES THE GARBAGE...
char[] a = new char[ecAnsi.GetCharCount(st.ToArray(), 0, (int)st.Length)];
ecAnsi.GetDecoder().GetChars(st.ToArray(), 0, (int)st.Length, a, 0);
// create a string from the char array
StringBuilder sb = new StringBuilder(a.Length);
foreach (char c in a) sb.Append(c);
Console.WriteLine(sb.ToString());
wr.Close();
--- end of sample code ---