2016-05-16 59 views
0

我正在使用WebClient類中的DownloadString從頁面中讀取內容,然後使用StreamWriter類將其內容寫入靜態HTML文件。在我正在閱讀的頁面中,有一個內聯javascript方法,它只是設置一個錨元素的OnClick屬性來設置我在查看靜態HTML頁面時發現的window.location = history.go(-1);,有一個奇怪的字母顯示不是呈現在動態網頁上。DownloadString然後寫入頁面問題

WebClient的& SteamWriter代碼

using (var client = new WebClient()) 
     { 
      var html = client.DownloadString(url); 

      //This constructor prepares a StreamWriter (UTF-8) to write to the specified file or will create it if it doesn't already exist 
      using (var stream = new StreamWriter(file, false, Encoding.UTF8)) 
      { 
       stream.Write(html); 
       stream.Close(); 
      } 
     } 

動態網頁的HTML代碼段的問題

<span>Sorry, but something went wrong on our end. &nbsp;Click <a href="#" onclick="window.location.href = history.go(-1);">here</a> to go back to the previous page.</span> 

靜態頁面的HTML代碼段

<span>Sorry, but something went wrong on our end. Â&nbsp;Click <a href="#" onclick="window.location.href = history.go(-1);">here</a> to go back to the previous page.</span> 

我在想,加入Encoding.UTF8參數可以解決這個問題,但它似乎沒有幫助。我需要做某種額外的編碼或解碼嗎?還是我完全錯過了這種類型的操作所需的其他東西?

+0

或許這就是你看到的http://stackoverflow.com/questions/1461907/html-encoding-issues-%C3%82-character-showing-up-instead-of-nbsp –

回答

0

我更新了WebClient以UTF8編碼,因爲它將資源轉換爲字符串,似乎已經解決了這個問題。

  using (var client = new WebClient()) 
      { 
       client.Encoding = System.Text.Encoding.UTF8; 
       var html = client.DownloadString(url); 

       //This constructor prepares a StreamWriter (UTF-8) to write to the specified file or will create it if it doesn't already exist 
       using (var stream = new StreamWriter(file, false, Encoding.UTF8)) 
       { 
        stream.Write(html); 
        stream.Close(); 
       } 
      }