WebUtility.HtmlDecode和HttpUtility.HtmlDecode无法正常工作?

时间:2016-10-08 14:30:43

标签: c# asp.net-mvc

我有以下代码无效..

var sampleText = "Mahindra & Mahindra announced that it is banking on its tractor business in the US and it expects to double its revenues to $1 billion in the ...";
var webDecoded = WebUtility.HtmlDecode(sampleText);
var httpDecoded = HttpUtility.HtmlDecode(sampleText);

以下是输出:

WebDecoded

HttpDecoded

正确的输出应该是:Mahindra&马辛德拉宣布......

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

如果您尝试获取Mahindra & Mahindra announced,则正确的输入应为Mahindra & Mahindra announced

您还有一个额外的amp;Mahindra & Mahindra announced

答案 1 :(得分:1)

代码正常运行。这个输入字符串:

"&"

当HTML解码时,会产生此输出字符串:

"&"

因为&解码为&

字符串中有一个额外的amp;,它不会解码为任何东西,因为它不是HTML代码。所以解码器就像任何其他文本一样对待它。

该输出字符串可以再次解码,这会将生成的&转换为&