get text from HTML - CSharp System

CSharp examples for System:String HTML

Description

get text from HTML

Demo Code


using System.Xml;
using System.Web;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*w w w .j a  v  a  2 s  . co  m*/

public class Main{
        /// <summary>
        /// get text from HTML
        /// </summary>
        /// <param name="source">HTML</param>
        /// <returns>text</returns>
        public static string StripHtml(this string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return string.Empty;
            }

            var content = Regex.Replace(source, @"<(script|style).*?</\1>", string.Empty);
            content = Regex.Replace(content, @"<.*?>", string.Empty);
            content = HttpUtility.HtmlDecode(content);
            return content;
        }
}

Related Tutorials