2016-05-16 11 views
0

WebClientクラスのDownloadStringを使用してページのコンテンツを読み込み、StreamWriterクラスを使用して静的なHTMLファイルに内容を書き込んでいます。私が読んでいるページには、アンカー要素のOnClick属性を設定するインラインJavaScriptスクリプトがあります。window.location = history.go(-1);静的なHTMLページを見ると、奇妙な文字が表示されます動的なウェブページ上に存在する。静的ページのHTMLスニペットDownloadStringを発行してページの問題に書き込む

<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> 

問題の

の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> 

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(); 
       } 
      } 
関連する問題