Solved

.Net - set the encoding on a TextWriter object

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

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…
Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…

815 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

8 Experts available now in Live!

Get 1:1 Help Now