【问题标题】:HttpUtility.HtmlDecode() fails for some characters某些字符的 HttpUtility.HtmlDecode() 失败
【发布时间】:2014-07-27 00:35:52
【问题描述】:

查看此代码:

namespace TestHtmlDecode
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Web;

    [TestClass]
    public class TestHtmlDecode
    {
        private string Convert(string input)
        {
            return HttpUtility.HtmlDecode(input);
        }

        [TestMethod]
        public void TestLeftBrace()
        {
            Assert.AreEqual("{", Convert("{"));
        }

        [TestMethod]
        public void TestGreaterThan()
        {
            Assert.AreEqual(">", Convert(">"));
        }
    }
}

TestGreaterThan 通过,但TestLeftBrace 失败(Convert 返回{)。这是为什么呢?

【问题讨论】:

    标签: c# string unicode character-encoding character-entities


    【解决方案1】:

    看起来这里发生了两件事。

    1. &lbrace 是 { 而不是 [ (http://jsfiddle.net/B7AAh/1/)

    2. &lbrace 似乎没有包含在已知项目列表中。源代码在此处http://referencesource.microsoft.com/#System/net/System/Net/WebUtility.cs,它指的是在此处找到的实体列表http://www.w3.org/TR/REC-html40/sgml/entities.html

    【讨论】:

    • 1.糟糕,这是个错误:) 2. 你知道我可以用什么来代替HtmlDecode()吗?
    • 对不起,我什么都不知道。对它进行调用然后有一个辅助列表可能会非常简单吗?想开始一个新项目:)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    相关资源
    最近更新 更多