开发者

Strip ALL HTML from a String?

I've seen regex that can remove tags, which is great, but I also have stuff like

 

etc.

This isn't actually from a HTML file. It's actually from a string. I'm pulling down data from SharePoint web services, which gives me the HTML users might use/get generated like

<div>Hello! Please remember to clean the break room!!! &quot;bob&quote; <B开发者_如何学CR> </div>

So, I'm parsing through 100-900 rows with 8-20 columns each.


Take a look at the HTML Agility Pack, it's an HTML parser that you can use to extract the InnerText from HTML nodes in a document.

As has been pointed out many times here on SO, you can't trust HTML parsing to a regular expression. There are times when it might be considered appropriate (for extremely limited tasks); but in general, HTML is too complex and too prone to irregularity. Bad things can happen when you try to parse HTML with Regular Expressions.

Using a parser such as HAP gives you much more flexibility. A (rough) example of what it might look like to use it for this task:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.Load("path to your HTML document");

StringBuilder content = new StringBuilder();
foreach (var node in doc.DocumentNode.DescendantNodesAndSelf())
{
    if (!node.HasChildNodes)
    {
        sb.AppendLine(node.InnerText);
    }
}

You can also perform XPATH queries on your document, in case you're only interested in a specific node or set of nodes:

var nodes = doc.DocumentNode.SelectNodes("your XPATH query here");

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜