/// <summary> /// Converts HTML to plain text. /// </summary> /// <remarks> /// Warning, the returned text may contain HTML fragments!!! /// For example, the original HTML code is: /// <code> /// <p>&lt;hello&lt;>/p> /// </code> /// Then, the converted text will be: /// <code> /// <hello> /// </code> /// So if you intend to output the stripped text in HTML, you need encode it first! /// </remarks> /// <param name="input">Input HTML. May be <c>null</c>.</param> /// <param name="newLine">The line terminator string</param> /// <returns>Plain text, may contain angle braces!</returns> public static string RenderToPlainText(string input, string newLine = "\n") { if (input == null) { return null; } var inputReader = new StringReader(input); var resultWriter = new StringWriter(); resultWriter.GetStringBuilder().EnsureCapacity(input.Length); resultWriter.NewLine = newLine; var textRenderer = new PlainTextRenderer(); textRenderer.Sanitize(inputReader, resultWriter); return resultWriter.ToString(); }
/// <summary> /// Converts HTML to plain text. /// </summary> /// <remarks> /// Warning, the returned text may contain HTML fragments!!! /// For example, the original HTML code is: /// <code> /// <p>&lt;hello&lt;>/p> /// </code> /// Then, the converted text will be: /// <code> /// <hello> /// </code> /// So if you intend to output the stripped text in HTML, you need encode it first! /// </remarks> /// <param name="input">Input HTML. May be <c>null</c>.</param> /// <param name="newLine">The line terminator string</param> /// <returns>Plain text, may contain angle braces!</returns> public static string RenderToPlainText(string input, string newLine = "\n") { if (input == null) { return(null); } var inputReader = new StringReader(input); var resultWriter = new StringWriter(); resultWriter.GetStringBuilder().EnsureCapacity(input.Length); resultWriter.NewLine = newLine; var textRenderer = new PlainTextRenderer(); textRenderer.Sanitize(inputReader, resultWriter); return(resultWriter.ToString()); }