我在C#(.Net 2.0)中使用Visual Studio社区。 我在WebBrowser控件中加载的网页上读取数据然后在WebBrowser控件中显示它时遇到问题。 该网页使用“iso-8859-1”字符集。 我已尝试使用“Document.Encoding”,您可以通过以下代码中的注释行看到,但它会一直显示这些带有白色问号的黑色losanges而不是法语字符。 我知道,这个程序没有任何用处,它只是一个向你展示问题的例子。 我究竟做错了什么? 我花了三天时间寻找解决方案。
private void button1_Click(object sender, EventArgs e)
{
string myquery = "";
string html = "";
string results = "";
int start;
int end;
//string encodage = "";
webBrowser1.ScriptErrorsSuppressed = true;
myquery = "http://citoyens.soquij.qc.ca/index.php";
webBrowser1.Navigate(myquery);
do {
Application.DoEvents();
} while (webBrowser1.ReadyState != WebBrowserReadyState.Complete);
//encodage = webBrowser1.Document.Encoding;
//MessageBox.Show(encodage);
html = webBrowser1.DocumentText;
start = html.IndexOf("a class=\"petit_logo\"");
start = html.IndexOf(">", start);
end = html.IndexOf("<", start);
results += html.Substring(start, end - start);
//webBrowser1.Document.Encoding = encodage;
webBrowser1.DocumentText = results;
//MessageBox.Show(webBrowser1.Document.Encoding);
}
答案 0 :(得分:0)
这里的问题是文档流没有用所需的编码格式正确编码。在文档以所需的编码格式编码后,您可能必须使用document.write,如下所示。
WebBrowser webBrowser1 = new WebBrowser();
string myquery = "";
string html = "";
string results = "";
int start;
int end;
//string encodage = "";
webBrowser1.ScriptErrorsSuppressed = true;
myquery = "http://citoyens.soquij.qc.ca/index.php";
webBrowser1.Navigate(myquery);
do
{
Application.DoEvents();
} while (webBrowser1.ReadyState != WebBrowserReadyState.Complete);
// For UTF-8 encoding.
//StreamReader sr = new StreamReader(webBrowser1.DocumentStream, Encoding.GetEncoding("UTF-8"));
//string source = sr.ReadToEnd();
//string encodage = webBrowser1.Document.Encoding;
//MessageBox.Show(encodage);
// Similarly, for "iso-8859-1" character set encoding.
using (StreamReader sr = new StreamReader(webBrowser1.DocumentStream, Encoding.GetEncoding(webBrowser1.Document.Encoding)))
{
string source = sr.ReadToEnd();
webBrowser1.Document.Write(source);
}
html = webBrowser1.DocumentText;
start = html.IndexOf("a class=\"petit_logo\"");
start = html.IndexOf(">", start);
end = html.IndexOf("<", start);
results += html.Substring(start, end - start);
//webBrowser1.Document.Encoding = encodage;
webBrowser1.DocumentText = results; //use Document.write(text) method.