WebUtility.HtmlDecode vs HttpUtilty.HtmlDecode

zi3*_*guw 23 .net html c# windows-phone

WebUtilty.HtmlDecode用来解码HTML.事实证明它没有正确解码,例如,–应该解码为" - "字符,但WebUtilty.HtmlDecode不解码它.HttpUtilty.HtmlDecode但是,确实如此.

Debug.WriteLine(WebUtility.HtmlDecode("–"));
Debug.WriteLine(HttpUtility.HtmlDecode("–"));


> –
> –
Run Code Online (Sandbox Code Playgroud)

解码截图

这两者的文档是相同的: 将已经过HTML编码的HTTP传输的字符串转换为已解码的字符串.

为什么它们不同,我应该使用哪一个,如果我切换到WebUtility.HtmlDecode以使" - "正确解码,会发生什么变化?

Kev*_*sse 12

Windows Phone上两种方法的实现确实不同.

WebUtility.HtmlDecode:

public static void HtmlDecode(string value, TextWriter output)
{
    if (value != null)
    {
        if (output == null)
        {
            throw new ArgumentNullException("output");
        }
        if (!StringRequiresHtmlDecoding(value))
        {
            output.Write(value);
        }
        else
        {
            int length = value.Length;
            for (int i = 0; i < length; i++)
            {
                bool flag;
                uint num4;
                char ch = value[i];
                if (ch != '&')
                {
                    goto Label_01B6;
                }
                int num3 = value.IndexOfAny(_htmlEntityEndingChars, i + 1);
                if ((num3 <= 0) || (value[num3] != ';'))
                {
                    goto Label_01B6;
                }
                string entity = value.Substring(i + 1, (num3 - i) - 1);
                if ((entity.Length <= 1) || (entity[0] != '#'))
                {
                    goto Label_0188;
                }
                if ((entity[1] == 'x') || (entity[1] == 'X'))
                {
                    flag = uint.TryParse(entity.Substring(2), NumberStyles.AllowHexSpecifier, NumberFormatInfo.InvariantInfo, out num4);
                }
                else
                {
                    flag = uint.TryParse(entity.Substring(1), NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out num4);
                }
                if (flag)
                {
                    switch (_htmlDecodeConformance)
                    {
                        case UnicodeDecodingConformance.Strict:
                            flag = (num4 < 0xd800) || ((0xdfff < num4) && (num4 <= 0x10ffff));
                            goto Label_0151;

                        case UnicodeDecodingConformance.Compat:
                            flag = (0 < num4) && (num4 <= 0xffff);
                            goto Label_0151;

                        case UnicodeDecodingConformance.Loose:
                            flag = num4 <= 0x10ffff;
                            goto Label_0151;
                    }
                    flag = false;
                }
            Label_0151:
                if (!flag)
                {
                    goto Label_01B6;
                }
                if (num4 <= 0xffff)
                {
                    output.Write((char) num4);
                }
                else
                {
                    char ch2;
                    char ch3;
                    ConvertSmpToUtf16(num4, out ch2, out ch3);
                    output.Write(ch2);
                    output.Write(ch3);
                }
                i = num3;
                goto Label_01BD;
            Label_0188:
                i = num3;
                char ch4 = HtmlEntities.Lookup(entity);
                if (ch4 != '\0')
                {
                    ch = ch4;
                }
                else
                {
                    output.Write('&');
                    output.Write(entity);
                    output.Write(';');
                    goto Label_01BD;
                }
            Label_01B6:
                output.Write(ch);
            Label_01BD:;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

HttpUtility.HtmlDecode:

public static string HtmlDecode(string html)
{
    if (html == null)
    {
        return null;
    }
    if (html.IndexOf('&') < 0)
    {
        return html;
    }
    StringBuilder sb = new StringBuilder();
    StringWriter writer = new StringWriter(sb, CultureInfo.InvariantCulture);
    int length = html.Length;
    for (int i = 0; i < length; i++)
    {
        char ch = html[i];
        if (ch == '&')
        {
            int num3 = html.IndexOfAny(s_entityEndingChars, i + 1);
            if ((num3 > 0) && (html[num3] == ';'))
            {
                string entity = html.Substring(i + 1, (num3 - i) - 1);
                if ((entity.Length > 1) && (entity[0] == '#'))
                {
                    try
                    {
                        if ((entity[1] == 'x') || (entity[1] == 'X'))
                        {
                            ch = (char) int.Parse(entity.Substring(2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture);
                        }
                        else
                        {
                            ch = (char) int.Parse(entity.Substring(1), CultureInfo.InvariantCulture);
                        }
                        i = num3;
                    }
                    catch (FormatException)
                    {
                        i++;
                    }
                    catch (ArgumentException)
                    {
                        i++;
                    }
                }
                else
                {
                    i = num3;
                    char ch2 = HtmlEntities.Lookup(entity);
                    if (ch2 != '\0')
                    {
                        ch = ch2;
                    }
                    else
                    {
                        writer.Write('&');
                        writer.Write(entity);
                        writer.Write(';');
                        continue;
                    }
                }
            }
        }
        writer.Write(ch);
    }
    return sb.ToString();
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,WP7上不存在WebUtility.此外,WebUtility的WP8实现与桌面实现相同.桌面实现HttpUtility.HtmlDecode只是一个包装WebUtility.HtmlDecode.最后但同样重要的HttpUtility.HtmlDecode是,Silverlight 5具有与Windows Phone 相同的实现,并且不实现WebUtility.

从那里,我可以猜测:由于Windows Phone 7运行时基于Silverlight,WP7继承了Silverlight版本HttpUtility.HtmlDecode,并且WebUtility不存在.然后是WP8,其运行时基于WinRT.WinRT带来了WebUtility,并HttpUtility.HtmlDecode保留了旧版本以确保与传统WP7应用程序的兼容性.

至于知道你应该使用哪一个...如果你想要目标WP7那么你别无选择,只能使用HttpUtility.HtmlDecode.如果您的目标是WP8,那么只需选择最适合您需求的方法.WebUtility可能是面向未来的选择,以防微软决定在即将推出的Windows Phone版本中放弃Silverlight运行时.但我只是选择HttpUtility的实际选择,而不必担心手动支持你提出的问题的例子.


小智 5

方法完全相同。而且,如果您尝试对它们进行反编译,那么这些实现似乎只是从另一个复制而来。

区别仅是预期用途HttpUtility包含在System.Web程序集中,并且有望在基于此程序集构建的ASP.net应用程序中使用。WebUtility包含在System几乎所有应用程序引用的程序集中,并提供给更多通用或客户使用。

  • @JanDobkowski代码在.NET的桌面版本上可能是相同的(我尚未检查),但是在Windows Phone 8上却明显不同 (10认同)
  • @argaz我刚刚再次对它进行了测试,并使用控制台应用程序,您是对的-它可以工作,但是在Windows Phone应用程序WebUtilty(新的全新)中,HtmlDecode不会解码`&#8211; (3认同)

cre*_*7or 5

只是为了通知其他人会在搜索中找到这个。使用问题中提到的任何函数,但不要使用Windows.Data.Html.HtmlUtilities.ConvertToText(string input). 它比WebUtilty.HtmlDecode并产生崩溃慢 70 倍!崩溃将mshtml!IEPeekMessage在 DevCenter 中命名。看起来这个函数调用 InternetExplorer 来转换字符串。只是避免它。