Solved

.Net - set the encoding on a TextWriter object

Posted on 2003-11-04
3
2,151 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Turn A Profile Picture Into A Cartoon Using Photoshop And Illustrator This tutorial will teach you how to make a cartoon style image out of a regular picture. I have tried to keep the tutorial as simple as possible. I used Adobe CS4 for this tuto…
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.
The purpose of this video is to demonstrate how to create a Printer Friendly PDF on a WordPress Page. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome Screenshot” Google Chrome Extension, and SmallPDF.com Log…
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now