加载时 C# HtmlAgilityPack 编码问题 html

C# HtmlAgilityPack encoding problem when loads html

我正在使用 HTMLAGILITYPACK 从 HTML 本地页面中提取所有数据,但仍然存在特殊字符问题...看一下:https://prnt.sc/p7xegw(我可以不要使用 ~、`、´、ç 和其他)。

这是调用 HTML 页面的代码:

System.IO.StreamReader myFile = new System.IO.StreamReader("C:/Users/Dev-02/Desktop/Daniel/xml_project/schemaproject/portal.html");

string arquivohtml = myFile.ReadToEnd();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();

doc.LoadHtml(arquivohtml);

有没有办法在我调用 HTML 页面时设置我的编码?

您需要将编码传递给 StreamReader 构造函数,它会解析文件的字节。