visibility 21 comment 0 access_time 2 months ago language English

codeHTML Parsing using HTML Agility Pack .NET

HTML Agility Pack (HAP) is one of the most commonly used .NET package to parse HTML. It creates a document object model in memory, which can be use to manipulate the nodes (including both elements and attributes). 

The package can be added to your project from NuGet via the following CLI:

dotnet add package HtmlAgilityPack --version 1.11.43

This code snippet provides one example of using this HAP to remove all script elements. The script can be run as C# script

For more details, refer to Html Agility Pack.

Code snippet

using HtmlAgilityPack;
using System.Linq;

var html = @"
<p>This is a test paragraph.</p>
<script type='text/javascript'>
var a =1;
console.log(a);
</script>
";

// From String
var doc = new HtmlDocument();
doc.LoadHtml(html);

// Clean out all script elements
var scriptElements = doc.DocumentNode.Descendants("script");
if(scriptElements !=null){
	foreach(var el in scriptElements.ToList()){
		el.Remove();
    }
}
fork_right Fork
copyright This page is subject to Site terms.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

comment Comments
No comments yet.