简体   繁体   中英

UTF-8 to ANSI Conversion using C#

I'm a .NET developer and was asked to do an application that converts html files to ANSI in C#.

ANSI is necessary because the converted files will run on a Visual Fox Pro application.

The basic logic is ready the problem is with the conversion itself.

I've tried this code: http://social.msdn.microsoft.com/Forums/pt-BR/026ddda3-9bd1-4502-b445-e2a1cc88345d/convert-file-from-utf8-to-ansi?forum=csharplanguage but when I checked it on editplus the file is still not converted to ANSI and even worst the indentation it's all messed up.

What I should do is to convert a file like editplus does, it preserves the document indentation and can convert any file from UTF8 to ANSI.

The whole point is that I'm working with hundreds of html files, so I can't just do it one by one using a text editor.

How can the conversion be done?

Is there a way to convert it and preserve the indentation like editplus does?

For the special characters like: "ã, ão, é, í..." I'm correcting it before the conversion. Is this the right approach?

Use Default Encoding instead of ASCII:

StreamReader sr = new StreamReader(infile);  
StreamWriter sw = new StreamWriter(outfile, false, Encoding.Default);  

// invoke the ReadToEnd method
sw.WriteLine(sr.ReadToEnd());  

sw.Close();  
sr.Close(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM