1

我正在使用 .net 4.5 和

HttpUtility.HtmlDecode无法解码&#39哪个是单引号字符

知道为什么吗?

在 Windows 8.1 上使用 C# .net 4.5 WPF

这里是失败的文本

Apple 13&#39&#39 Z0RA30256 MacBook Pro Retina

以下是框架版本

#region Assembly System.Web.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Web.dll
#endregion
4

1 回答 1

2

使用内置的 it 方法无法处理此问题HtmlDecode,您必须找到/替换它或以其他方式解决。

下面是源代码HtmlDecode- 您可以从评论中明确看到您的方案已被考虑但不支持 - HTML 实体必须以 a 为界;,否则它们根本不是 HTML 实体。浏览器可以原谅不正确的标记,并相应地进行补偿。

// We found a '&'. Now look for the next ';' or '&'. The idea is that
// if we find another '&' before finding a ';', then this is not an entity,
// and the next '&' might start a real entity (VSWhidbey 275184)

如果您想调整行为,这里是 .NET 的完整来源HtmlDecodeHttpUtility

http://referencesource.microsoft.com/#System/net/System/Net/WebUtility.cs,44d08941e6aeb00d

public static void HtmlDecode(string value, TextWriter output)
{
    if (value == null)
    {
        return;
    }
    if (output == null)
    {
        throw new ArgumentNullException("output");
    }

    if (value.IndexOf('&') < 0)
    {
        output.Write(value);        // good as is
        return;
    }

    int l = value.Length;
    for (int i = 0; i < l; i++)
    {
        char ch = value[i];

        if (ch == '&')
        {
            // We found a '&'. Now look for the next ';' or '&'. The idea is that
            // if we find another '&' before finding a ';', then this is not an entity,
            // and the next '&' might start a real entity (VSWhidbey 275184)
            int index = value.IndexOfAny(_htmlEntityEndingChars, i + 1);
            if (index > 0 && value[index] == ';')
            {
                string entity = value.Substring(i + 1, index - i - 1);

                if (entity.Length > 1 && entity[0] == '#')
                {
                    // The # syntax can be in decimal or hex, e.g.
                    //      å  --> decimal
                    //      å  --> same char in hex
                    // See http://www.w3.org/TR/REC-html40/charset.html#entities

                    ushort parsed;
                    if (entity[1] == 'x' || entity[1] == 'X')
                    {
                        UInt16.TryParse(entity.Substring(2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out parsed);
                    }
                    else
                    {
                        UInt16.TryParse(entity.Substring(1), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out parsed);
                    }

                    if (parsed != 0)
                    {
                        ch = (char)parsed;
                        i = index; // already looked at everything until semicolon
                    }
                }
                else
                {
                    i = index; // already looked at everything until semicolon

                    char entityChar = HtmlEntities.Lookup(entity);
                    if (entityChar != (char)0)
                    {
                        ch = entityChar;
                    }
                    else
                    {
                        output.Write('&');
                        output.Write(entity);
                        output.Write(';');
                        continue;
                    }
                }

            }
        }

        output.Write(ch);
    }
}
于 2014-12-20T17:00:57.103 回答