在正文中获取简单文本

本文关键字:简单 文本 获取 正文 | 更新日期: 2023-09-27 17:50:34

我想在body标签中获得纯文本。

标记:

**simple text 1**
<div>------</div>
<font>-------</font>
**simple text 2**
代码:

foreach (HtmlElement elm in webBrowser1.Document.Body.All)
{
    //get simple text
}

在正文中获取简单文本

简体:

string plainText = webBrowser1.Document.Body.InnerText;

我发现有一个简单的方法:

HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(webBrowser1.Document.Body.InnerHtml);
foreach (var elm in htmlDoc.DocumentNode.Descendants())
{
    if (elm.NodeType == HtmlNodeType.Text)
    {
        //simple text is #text
        var innerText=elm.InnerText;
    }  
}

尝试以下方法:您可以使用以下技术获得浏览器预览中显示的所有文本。

        string plainText= StripHTML(webBrowser1);// call this way-----
        public string StripHTML(WebBrowser webp)
        {
            try
            {
                Clipboard.Clear();
                webp.Document.ExecCommand("SelectAll", true, null);
                webp.Document.ExecCommand("Copy", false, null);
            }
            catch (Exception ep)
            {
                MessageBox.Show(ep.Message);
            }
            return Clipboard.GetText();            
        }