Click here to Skip to main content
16,012,028 members
Home / Discussions / C#
   

C#

 
AnswerRe: problems in checklistbox Pin
Christian Wikander16-May-08 2:25
Christian Wikander16-May-08 2:25 
QuestionHow can i read print content (print document data) from a Job using PrintQueueStream [modified] Pin
Krishna Prasad RVS16-May-08 0:38
Krishna Prasad RVS16-May-08 0:38 
QuestionXML encoding issue Pin
George_George16-May-08 0:11
George_George16-May-08 0:11 
AnswerRe: XML encoding issue Pin
mlsteeves16-May-08 3:48
mlsteeves16-May-08 3:48 
GeneralRe: XML encoding issue Pin
George_George16-May-08 4:49
George_George16-May-08 4:49 
GeneralRe: XML encoding issue Pin
mlsteeves16-May-08 5:22
mlsteeves16-May-08 5:22 
GeneralRe: XML encoding issue Pin
George_George16-May-08 20:37
George_George16-May-08 20:37 
GeneralRe: XML encoding issue Pin
mlsteeves20-May-08 1:55
mlsteeves20-May-08 1:55 
With your code sample, you are missing the part to tells the XmlTextWriter what encoding to use. If you use any class that is derived from a TextWriter (like StringWriter), then you can't specify the encoding. The reason for this is that the base string in a StringWriter is UTF-16, so you have no options for using a different Encoding.

If however, you use a MemoryStream, or something derived directly from Stream, then you can specify a different Encoding.

Anyway, here is a code snippet that describes this:
MemoryStream ms = new MemoryStream();

//Set the encoding to UTF8:
XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8);

//Just makes the xml easier to read:
writer.Formatting = Formatting.Indented;

//Write out our xml document:
writer.WriteStartDocument();
writer.WriteStartElement("Stock");
writer.WriteAttributeString("Symbol", "123");
writer.WriteElementString("Price", "456");
writer.WriteElementString("Change", "abc");
writer.WriteElementString("Volume", "edd");
writer.WriteEndElement();

//Reset our stream's read pointer, so we can read back from our memory stream:
writer.Flush();
ms.Seek(0, SeekOrigin.Begin);

//Read our memory stream, and output to console:
StreamReader sr = new StreamReader(ms);
string content = sr.ReadToEnd();
Console.WriteLine(content);

return;


It is important to note that you could have used a similar technique in your original code when you used the XmlDocument.

The reason why you were getting the UTF-16 encoding is because your underlying writer class was a string. StringWriter writes directly to a string (or possibly a StringBuilder). And because strings in .NET are all UTF-16, that is the encoding you got.

When you write directly to a stream (FileStream, MemoryStream, etc), then you are not writing to a string, but conceptually you are writing to just an array of bytes. Because of that you can specify a different encoding.

Anyway, I hope this helps you out.
GeneralRe: XML encoding issue Pin
George_George20-May-08 3:55
George_George20-May-08 3:55 
GeneralRe: XML encoding issue Pin
PIEBALDconsult16-May-08 5:52
mvePIEBALDconsult16-May-08 5:52 
GeneralRe: XML encoding issue Pin
George_George16-May-08 20:39
George_George16-May-08 20:39 
GeneralRe: XML encoding issue Pin
PIEBALDconsult17-May-08 4:01
mvePIEBALDconsult17-May-08 4:01 
GeneralRe: XML encoding issue Pin
George_George17-May-08 4:13
George_George17-May-08 4:13 
GeneralRe: XML encoding issue Pin
PIEBALDconsult17-May-08 7:58
mvePIEBALDconsult17-May-08 7:58 
GeneralRe: XML encoding issue Pin
George_George17-May-08 21:33
George_George17-May-08 21:33 
GeneralRe: XML encoding issue Pin
PIEBALDconsult18-May-08 4:48
mvePIEBALDconsult18-May-08 4:48 
GeneralRe: XML encoding issue Pin
George_George20-May-08 3:51
George_George20-May-08 3:51 
GeneralRe: XML encoding issue Pin
PIEBALDconsult20-May-08 14:16
mvePIEBALDconsult20-May-08 14:16 
GeneralRe: XML encoding issue Pin
George_George20-May-08 22:53
George_George20-May-08 22:53 
QuestionSend Object By Value? Pin
Harvey Saayman16-May-08 0:08
Harvey Saayman16-May-08 0:08 
AnswerRe: Send Object By Value? Pin
Vikram A Punathambekar16-May-08 0:13
Vikram A Punathambekar16-May-08 0:13 
QuestionSHGetFileInfo does not work for large icons Pin
nilam247715-May-08 23:54
nilam247715-May-08 23:54 
AnswerRe: SHGetFileInfo does not work for large icons Pin
DaveyM6916-May-08 3:16
professionalDaveyM6916-May-08 3:16 
AnswerRe: SHGetFileInfo does not work for large icons Pin
DaveyM6916-May-08 3:25
professionalDaveyM6916-May-08 3:25 
QuestionAbout grid checkBox Pin
Prabhat00315-May-08 23:51
Prabhat00315-May-08 23:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.