2014-10-29 29 views
0

閱讀HTML身體下面我有HTML文件,它包含的內容象下面這樣:垃圾的價值,同時使用C#

<HTML> 
<BODY> 
... 
........ company's Chief Financial Officer. Now the....... 
... 
</BODY> 
</HTML> 

我使用閱讀本文件的內容:

StringBuilder stringBuilder = new StringBuilder(); 
using (StreamReader sr = new StreamReader(filePath)) 
{ 
    String line = sr.ReadToEnd(); 
    stringBuilder.Append(line); 
} 
strFileContent = stringBuilder.ToString(); 

但是它返回字符串爲:

........company sChief FinancialOfficer. 現在..... ..

HTML文件在我的本地系統中。

+1

什麼是文件的編碼?嘗試明確指定編碼,否則'StreamReader'將默認爲'UTF8'。 – 2014-10-29 06:43:02

+0

@ Sriram,目前的編碼是charset = windows-1252。我認爲這是造成問題 – Aquarius24 2014-10-29 06:50:32

回答

2

您需要使用它來創建文件相同的編碼。 StreamReader默認情況下您的編碼是UTF8,並嘗試使用該編碼對文件進行解碼,但原始編碼爲windows-1252(如您在註釋中所述)。嘗試使用錯誤的編碼讀取會產生垃圾數據,原因很明顯。

你應該明確地說出文件的編碼方式。下面是你如何做。

var encoding = Encoding.GetEncoding(1252);//windows-1252 
using (StreamReader sr = new StreamReader(filePath, encoding)) 
... 

Bonus reading

+0

謝謝!值得學習(Y) – Aquarius24 2014-10-29 07:04:43

0

必須設置編碼中的StreamReader這樣的:

using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8))