Solved

.Net - set the encoding on a TextWriter object

Posted on 2003-11-04
3
2,167 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
3 Comments
 
LVL 1

Accepted Solution

by:
AJMartin earned 30 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When deciding to adopt any help desk solutions many factors should be explored before taking decisions. This will change from business to another but in general there are some kind of rule of thumb. Here are some quick tips: Do we need only ticket…
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
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…

740 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