如一篇文章,html格式有&nbps;’等等很多
将其分别转成空格和大写的右引号有高效点的算法吗?

解决方案 »

  1.   

    最简单的就是用System.Web.HttpUtility.HtmlEncode(string)如果要一定的浏览器解释,则可以用用WebBrower控件去做Rendering:// 例子可能要IE7以上。
    private void button1_Click(object sender, EventArgs e)
    {
        WebBrowser browser = new WebBrowser();
        browser.DocumentText = "<html><body><h2>hi</h2>&nbsp;&lsquo;nihao&rsquo;&nbsp;</body></html>";
        Application.DoEvents();
        browser.Document.ExecCommand("SelectAll", false, System.Reflection.Missing.Value);
        browser.Document.ExecCommand("Copy", false, System.Reflection.Missing.Value);
        if (Clipboard.ContainsText(TextDataFormat.Text))
        {
            MessageBox.Show(Clipboard.GetText(TextDataFormat.Text));
        }
    }
      

  2.   

    System.Web.HttpUtility.HtmlDecode(string)
      

  3.   

    gomoku
    方法很好。但是我引申一下,大家可能没在意过。.net本身的方法似乎效率很低,分析html其实不怎么花时间,对这个优化意义不大。关键在于下载源码。最近回帖几个vb6的问题。发现效率差距很大。
    public static void Test()
    {
        int tick = Environment.TickCount;
        string html = GetHtmlCom("http://www.csdn.net");
        Console.WriteLine(Environment.TickCount - tick);
        tick = Environment.TickCount;
        html = GetHtmlWebclient("http://www.csdn.net");
        Console.WriteLine(Environment.TickCount - tick);
    }public static string GetHtmlCom(string url)
    {
        XMLHTTP xmlhttp = new XMLHTTPClass();
        xmlhttp.open("get", url, false, null, null);
        xmlhttp.send("");
        while (xmlhttp.readyState != 4) Thread.Sleep(10);
        return xmlhttp.responseText;
    }public static string GetHtmlWebclient(string url)
    {
        return Encoding.UTF8.GetString(new WebClient().DownloadData(url));
    }
    输出
    47
    3089换个方法,下载页面快一点,才是优化的关键,毕竟对瓶颈优化才有意义。