?
Solved

.Net - set the encoding on a TextWriter object

Posted on 2003-11-04
3
Medium Priority
?
2,173 Views
Last Modified: 2007-12-19
My question is how to I set the encoding on a TextWriter object?

I'm passing the result of an XML/XSLT tranformation to it later in the script.

the encoding specified in the XSLT document is ignored and the TextWriter property is used instead.

Therefore I need to change it to what I need (I want to set it to UTF-8)

It's declared as follows:-

TextWriter writer = new StringWriter();


Thanks!




0
Comment
Question by:berni_brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Accepted Solution

by:
AJMartin earned 120 total points
ID: 9836230
Hi!

Internally, C# stores all characters as Unicode, IIRC. That's why you can't find an character encoding property for StringWriter and TextWriter.

It's only upon writing your document to disk or to a webserver (and then to a browser) that you have to worry about an encoding. In that case, you'll want to check out Encoding Class at: ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfsystemtextencodingclasstopic.htm
Quote:
The ASCIIEncoding class encodes Unicode characters as single 7-bit ASCII characters. This encoding only supports character values between U+0000 and U+007F.

The UnicodeEncoding class encodes each Unicode character as two consecutive bytes. Both little-endian (code page 1200) and big-endian (code page 1201) byte orders are supported.

The UTF7Encoding class encodes Unicode characters using the UTF-7 encoding (UTF-7 stands for UCS Transformation Format, 7-bit form). This encoding supports all Unicode character values, and can also be accessed as code page 65000.

The UTF8Encoding class encodes Unicode characters using the UTF-8 encoding (UTF-8 stands for UCS Transformation Format, 8-bit form). This encoding supports all Unicode character values, and can also be accessed as code page 65001.

The HttpWriter class at: ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfsystemwebhttpwritermemberstopic.htm and maybe the StreamWriter class at: ms-help://MS.NETFrameworkSDKv1.1/cpref/html/frlrfsystemiostreamwritermemberstopic.htm could also be helpful to you.

I hope that helps!
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question