0

我正在尝试在我的 WinForms 应用程序中将字符串从“英语到孟加拉语”翻译。我试过这段代码:

string input = "i eat rice";
string languagePair = "en|bn";

string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);
WebClient webClient = new WebClient();
webClient.Encoding = System.Text.Encoding.UTF8;
string result = webClient.DownloadString(url);
result = result.Substring(result.IndexOf("<span title=\"") + "<span title=\"".Length);
result = result.Substring(result.IndexOf(">") + 1);
result = result.Substring(0, result.IndexOf("</span>"));
MessageBox.Show(result.Trim());

但我得到的是:
&#2438 &#2478 &#2495 &#2477 &#2494 &#2468 &#2454 &#2494 &#2439
但如果我把它放在谷歌的搜索框中,那么它会在搜索框中显示我翻译的语言。如何让翻译后的语言显示在我的 WinForm 中?注意:我不想使用谷歌翻译 API。

4

1 回答 1

3

您得到的结果是&#...每个 UTF-16 字符的 HTML 实体编码。您可以使用HttpUtility.HtmlDecodeorWebUtility.HtmlDecode来获取实际的 unicode 字符串。

result = HttpUtitlityDecode(result.Trim());
MessageBox.Show(result);

有关更多详细信息,请参阅解码所有 HTML 实体

于 2013-01-09T04:34:11.040 回答