我在使用UTF-8
时将ISO-8859-1
转换为htmlagilitypack
时遇到问题
string url = "http://www.example.com";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
HtmlDocument htmldocument = new HtmlDocument();
htmldocument.Load(response.GetResponseStream(), Encoding.UTF8);
HtmlNode headnode = htmldocument.DocumentNode.SelectSingleNode("//head");
HtmlNode titlenode = headnode.SelectSingleNode(".//title");
string title = titlenode.InnerText;
标题输出为Nice Café
这就是我尝试编码的方式
Encoding iso = Encoding.GetEncoding("ISO-8859-1");
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(title);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
string Fixedtitle = iso.GetString(isoBytes);
固定标题输出应为Nice Café
我认为问题是当iso-8859-1被错误地强制进入UTF-8时。但我无法修复输出文本。
请帮帮我。最好的问候。