简体   繁体   English

使用 c# 将 html 转换为纯文本

[英]convert html to plain text using c#

i use this method to convert html to plaint text but it have some bugs in this html tags <H1,2,3,..>我使用此方法将 html 转换为纯文本,但在此 html 标签 <H1,2,3,..> 中有一些错误

Method:方法:

public string HtmlToPlainText(string htmlText)
    {
        //const string tagWhiteSpace = @"(>|$)(\W|\n|\r)+<";//matches one or more (white space or line breaks) between '>' and '<'
        const string stripFormatting = @"<[^>]*(>|$)";//match any character between '<' and '>', even when end tag is missing
        const string lineBreak = @"<(br|BR)\s{0,1}\/{0,1}>";//matches: <br>,<br/>,<br />,<BR>,<BR/>,<BR />
        var lineBreakRegex = new Regex(lineBreak, RegexOptions.Multiline);
        var stripFormattingRegex = new Regex(stripFormatting, RegexOptions.Multiline);
        //var tagWhiteSpaceRegex = new Regex(tagWhiteSpace, RegexOptions.Multiline);

        var text = htmlText;
        //Decode html specific characters
        text = System.Net.WebUtility.HtmlDecode(text);
        //Remove tag whitespace / line breaks
        //text = tagWhiteSpaceRegex.Replace(text, "><");
        //Replace < br /> with line breaks
        text = lineBreakRegex.Replace(text, Environment.NewLine);
        //Strip formatting
        text = stripFormattingRegex.Replace(text, string.Empty);
        return text;
    }

this is my html text :这是我的 html 文本

 <h3> This is a simple title </h3> </br> <p>Lorem ipsum <b> dolor sit </b> amet consectetur, <i>adipisicing elit.</i> </p>

This is my result :这是我的结果

This is a simple title Lorem ipsum dolor sit amet consectetur,这是一个简单的标题 Lorem ipsum dolor sit amet consectetur,
adipisicing elit.减肥精英。

The result should be :结果应该是

This is a simple title这是一个简单的标题

Lorem ipsum dolor sit amet consectetur, adipisicing elit. Lorem ipsum dolor 坐在 amet consectetur,adipisicing 精英。

I think the error is from Strip formatting.我认为错误来自条带格式。 How can i solve it?我该如何解决?

Parsing HTML is not an easy task (even for a subset of HTML).解析 HTML 并非易事(即使对于 HTML 的子集)。 If regex feels like a good solution for this task it is actually not that great.如果 regex 感觉是这个任务的一个很好的解决方案,它实际上并不是那么好。 To parse HTML, you should use... an HTML parser.要解析 HTML,您应该使用... HTML 解析器。 In C#, AngleSharp and the HTMLAgilityPack are the most common solution.在 C# 中, AngleSharpHTMLAgilityPack是最常见的解决方案。 Here is an example with AngleSharp:以下是 AngleSharp 的示例:

using System;
using AngleSharp;
using AngleSharp.Html.Parser;

class MyClass {
    static void Main() {
        //Use the default configuration for AngleSharp
        var config = Configuration.Default;

        //Create a new context for evaluating webpages with the given config
        var context = BrowsingContext.New(config);

        //Source to be parsed
        var source = @"<h3> This is a simple title </h3>
</br>
<p>Lorem ipsum <b> dolor sit </b> amet consectetur, <i>adipisicing elit.</i> </p>
";

        //Create a parser to specify the document to load (here from our fixed string)
        var parser = context.GetService<IHtmlParser>();
        var document = parser.ParseDocument(source);

        //Do something with document like the following
        Console.WriteLine(document.DocumentElement.TextContent);
    }
}

Try it Online在线试用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM