text encoding issues

  • Thread starter Thread starter Dan
  • Start date Start date
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 ---
 
The resulting string I'm getting seems to be OK:
sb.ToString() "<root>\r\n <item>I'm an item</item>\r\n</root>"
The only "extra" characters that appear in the string are \r\n end-of -line
sequences.
What device you are using? Do you you some 3rd-party console shell?

- Roman

This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
| From: "Dan" <[email protected]>
| Subject: text encoding issues
| Date: Tue, 25 Nov 2003 23:41:16 +0100
| Lines: 33
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <OEe$#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: ppp-217-133-158-65.cust-adsl.tiscali.it 217.133.158.65
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!cpmsftngxa09.phx.gbl!TK2MSFTNGP08.
phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:39362
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| 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 ---
|
|
|

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top