通过C#将Html utf-8字符集转换为ISO-8859-1 [英] Converting Html utf-8 charset to ISO-8859-1 via C#

查看:1444
本文介绍了通过C#将Html utf-8字符集转换为ISO-8859-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里是我尝试转换的HTML(确保字符集不会显示在这里,但是,我完全按照您的看法看到它)。

 < a href =https: //sistemas.usp.br/jupiterweb/listarGradeCurricular?codcg=12&amp;codcur=12012&amp;codhab=1&amp;tipo=Ntarget =_ blank>行政管理 - 圣保罗 -  diurno< / a> ; 

好的,这个htmlnode的VALUE是Administração - SãoPaulo - diurno。



我使用HtmlAgilityPack来解析HtmlPage,一旦我到达这个节点,它的innerText值就像这样:Administraçãoâ São Pauloâdiurno



我假设页面的原始字符集是UTF-8,因为那就是html上的编码标记对我说。



如何将这个奇怪的字符串转换为:Administração - SãoPaulo - Diurno

我已经试过这些线程:线程 thread two
并没有解决我的问题

编辑:我通过C#WebRequest获取页面Get。



EDIT2:添加了HtmlAgilityPack标签



问题是孤立的:WebRequest有时会搞乱Html。有没有其他的方法来设置编码?我在尝试:_webReq.Encoding =ISO-8859-1



预先感谢

解决方案

在一个小测试后,您可以看到该字符串没有正确地将Encoded恢复到原始形式。



示例测试:

  var item =管理 - 圣保罗 -  Diurno; 
Console.WriteLine(item);

var buffer = Encoding.UTF8.GetBytes(item);
var item2 = Encoding.Default.GetString(buffer);
Console.WriteLine(item2);

打印:

 Administraçao -  Sao Paulo  -  Diurno 
AdministraA§A£o - SA£o Paulo - Diurno

正如您所看到的,原始字符串正在使用 UTF8 ,然后使用默认编码。



这是错误的。

如果WebRequest.GetResponse()将字符串作为错误的值返回,则该方法存在问题。尝试将HttpWebRequest上的 TransferEncoding 属性设置为UTF8。


在设置TransferEncoding属性之前,必须先将SendChunked属性设置为true。通过将TransferEncoding设置为null来清除对SendChunked的值没有影响。分配给TransferEncoding属性的值将替换任何现有内容。


或者您可以尝试在打开的StreamReader上将Encoding设置为UTF8。我可以看到你的代码吗?


I've been struggling to convert a html value of an attribute, without any success.

Here is the the HTML i am trying to convert (sure the charset will not be shown here, but, i see it exactly as you see it).

<a href="https://sistemas.usp.br/jupiterweb/listarGradeCurricular?codcg=12&amp;codcur=12012&amp;codhab=1&amp;tipo=N" target="_blank">Administração – São Paulo – diurno</a>

All right, the VALUE of this htmlnode is "Administração - São Paulo - diurno".

I am using HtmlAgilityPack to parse the HtmlPage for this, and once i reach this node, its innerText value is just like this : Administração â São Paulo â diurno

I am assuming the original charset of the page is UTF-8 because thats what the encoding tag on the html says to me.

How can i convert this weird string to : Administração - São Paulo - Diurno ?

I've tried these threads already : thread one and thread two and nothing solved my issue

EDIT: I am getting the page via a C# WebRequest Get.

EDIT2 : Added HtmlAgilityPack tag

The problem was isolated : WebRequest is messing the Html sometimes.

Is there any other way to set the encoding ? i am trying : _webReq.Encoding = "ISO-8859-1"

Thanks in advance

解决方案

After a small test, you can see that the string is not properly getting Encoded back to its original form.

Sample test:

 var item = "Administração - São Paulo - Diurno";
 Console.WriteLine(item);

 var buffer = Encoding.UTF8.GetBytes(item);
 var item2 = Encoding.Default.GetString(buffer);
 Console.WriteLine(item2);

This prints:

Administraçao - Sao Paulo - Diurno
AdministraA§A£o - SA£o Paulo - Diurno

As you can see, the original string is being converted to bytes using UTF8, but then it is being converted back to a string using Default encoding.

This is wrong.

If WebRequest.GetResponse() is returning the string as the wrong value, then there is a problem with that method. Try setting the TransferEncoding property on the HttpWebRequest to UTF8.

Before you can set the TransferEncoding property, you must first set the SendChunked property to true. Clearing TransferEncoding by setting it to null has no effect on the value of SendChunked. Values assigned to the TransferEncoding property replace any existing contents.

Or you can try to set the Encoding to UTF8 on the StreamReader you open. Can I see your code?

这篇关于通过C#将Html utf-8字符集转换为ISO-8859-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆