Solved

.Net - set the encoding on a TextWriter object

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When setting up new project requests for our site, one of the most powerful tools our team has available to use is Axure (http://www.axure.com/). It’s a tool for creating software and web prototypes that can function and interact as if it were the a…
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 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 add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…

863 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

27 Experts available now in Live!

Get 1:1 Help Now