开发者

Removing <script></script> block from returned html in C#

开发者 https://www.devze.com 2023-04-08 12:50 出处:网络
I am returning html contents (page layout) in a variable but want to remove <开发者_StackOverflow中文版;script>blabla</script> tags and contents within these tags.

I am returning html contents (page layout) in a variable but want to remove <开发者_StackOverflow中文版;script>blabla</script> tags and contents within these tags.

How can I do this?


You really need to parse the HTML.

Try using the Html Agility Pack which should make this pretty straightforward, for example:

HtmlDocument doc = new HtmlDocument();
doc.Load("HTMLPage1.htm");
foreach (var node in doc.DocumentNode.SelectNodes("//script"))
{
    node.Remove();
}
0

精彩评论

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