HtmlSharp 1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package HtmlSharp --version 1.0.0
NuGet\Install-Package HtmlSharp -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HtmlSharp" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HtmlSharp --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HtmlSharp, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HtmlSharp as a Cake Addin #addin nuget:?package=HtmlSharp&version=1.0.0 // Install HtmlSharp as a Cake Tool #tool nuget:?package=HtmlSharp&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
How to use
1. Html block parser.
using System.Linq;
using HtmlSharp;
using HtmlSharp.Nodes.Tags;
using HtmlSharp.Nodes.Tags.Html;
namespace HtmlSharpDemo
{
class Program
{
static void Main(string[] args)
{
var html = @"<!DOCTYPE html>
<html>
<body>
<h4 class=""page-title-1"">Starter</h4>
<h4 class=""page-title"">Starter2</h4>
</body>
</html>";
HtmlParser htmlParser = new HtmlParser();
ITag tag = htmlParser.Parse(html);
var selectedTag = tag
.GetChildren()
.Where(x => x is H4 &&
x.GetAttribute("class") == "page-title-1").FirstOrDefault();
selectedTag.ToString();
selectedTag.Texts.FirstOrDefault().ValueAsString.ToString();
}
}
}
selectedTag.ToString();
> "<h4 class=\"page-title-1\" >Starter</h4>"
selectedTag.Texts.FirstOrDefault().ValueAsString.ToString();
> "Starter"
2. Remove specific child tag.
using System.Linq;
using HtmlSharp;
using HtmlSharp.Nodes.Tags;
using HtmlSharp.Nodes.Tags.Html;
using HtmlSharp.Extensions;
namespace HtmlSharpDemo
{
class Program
{
static void Main(string[] args)
{
var html = @"<!DOCTYPE html>
<html>
<body>
<h4 class=""page-title-1"">Starter</h4>
<h4 class=""page-title"">Starter2</h4>
</body>
</html>";
HtmlParser htmlParser = new HtmlParser();
ITag tag = htmlParser.Parse(html);
var selectedTag = tag
.GetChildren()
.Where(x => x is H4 &&
x.GetAttribute("class") == "page-title-1")
.FirstOrDefault();
(tag as Tag).RemoveChild(selectedTag);
tag.ToString();
}
}
}
Also you can avoid unboxing by calling "As" method.
using System.Linq;
using HtmlSharp;
using HtmlSharp.Nodes.Tags;
using HtmlSharp.Nodes.Tags.Html;
using HtmlSharp.Extensions;
namespace HtmlSharpDemo
{
class Program
{
static void Main(string[] args)
{
var html = @"<!DOCTYPE html>
<html>
<body>
<h4 class=""page-title-1"">Starter</h4>
<h4 class=""page-title"">Starter2</h4>
</body>
</html>";
HtmlParser htmlParser = new HtmlParser();
Tag tag = htmlParser.Parse(html).As<Tag>();
var selectedTag = tag
.GetChildren()
.Where(x => x is H4 &&
x.GetAttribute("class") == "page-title-1")
.FirstOrDefault();
tag.RemoveChild(selectedTag);
tag.ToString();
}
}
}
tag.ToString();
> "<html ><body ><h4 class=\"page-title\" >Starter2</h4></body></html>"
3. Remove similar tags.
using System.Linq;
using HtmlSharp;
using HtmlSharp.Nodes.Tags;
using HtmlSharp.Nodes.Tags.Html;
using HtmlSharp.Extensions;
namespace HtmlSharpDemo
{
class Program
{
static void Main(string[] args)
{
var html = @"<!DOCTYPE html>
<html>
<body>
<h4 class=""page-title-1"">Starter</h4>
<h4 class=""page-title"">Starter2</h4>
</body>
</html>";
HtmlParser htmlParser = new HtmlParser();
ITag tag = htmlParser.Parse(html);
var similarTags = tag
.GetChildren()
.Where(x => x is H4).ToList();
(tag as Tag).RemoveChildren(similarTags);
tag.ToString();
}
}
}
tag.ToString();
> "<html > <body ></body></html>"
4. Replace a specific tag of a specific position order with a new one.
using System.Linq;
using HtmlSharp;
using HtmlSharp.Nodes.Tags;
using HtmlSharp.Nodes.Tags.Html;
using HtmlSharp.Extensions;
using HtmlSharp.Nodes;
namespace HtmlSharpDemo
{
class Program
{
static void Main(string[] args)
{
var html = @"<!DOCTYPE html>
<html>
<body>
<ol class=""breadcrumb m-0"">
<li class=""breadcrumb-item""><a>Example 1</a></li>
<li class=""breadcrumb-item""><a>Example 2</a></li>
<li class=""breadcrumb-item""><a>Example 3</a></li>
</ol>
</body>
</html>";
HtmlParser htmlParser = new HtmlParser();
ITag tag = htmlParser.Parse(html);
var replaceableTag = tag.GetChildren()
.Where(x => x.GetAttribute("class") == "breadcrumb m-0")
?.FirstOrDefault()
?.GetChildren()
?.Where(x => x is Li)
?.ElementAtOrDefault(1);
Li newLi = new Li();
newLi.AppendAttribute("class", "breadcrumb-item")
.AppendTag(new HyperLink())
.AppendText(new Text("Example 2 from FLUENT API"));
(tag as Tag).ReplaceChild(replaceableTag, newLi);
tag.ToString();
}
}
}
tag.ToString();
> "<html ><body ><ol class=\"breadcrumb m-0\" ><li class=\"breadcrumb-item\" ><a >Example 1</a></li><li class=\"breadcrumb-item\" ><a ></a>Example 2 from FLUENT API</li><li class=\"breadcrumb-item\" ><a >Example 3</a></li></ol></body></html>"
5. User defined tags (a little deeper).
Except of predefined tags of the library, It is possible also to parse any type of XMLs or support future HTML versions with the probability of existence of new tags by using the user defined tag mechanism of the library. Also it is possible to create your own parser or exporter by resusing full components of the library.
using System.Collections.Generic;
using System.Linq;
using HtmlSharp.Extensions;
using HtmlSharp;
using HtmlSharp.Nodes.Tags;
using HtmlSharp.Nodes.Tags.Html;
namespace HtmlSharpDemo
{
public class MyList : Tag
{
public override string StartingTag => "<myList>";
public override string ClosingTag => "</myList>";
}
public class MyInput : SelfCloseTag
{
public override string StartingTag => "myInput";
}
class Program
{
static void Main(string[] args)
{
var html = @"<!DOCTYPE html>
<html>
<body>
<myList class=""breadcrumb m-0"">
<li class=""breadcrumb-item""><a>Example 1</a></li>
<li class=""breadcrumb-item""><a>Example 2</a></li>
<li class=""breadcrumb-item""><a>Example 3</a></li>
</myList>
<ol class=""myInput""/>
</body>
</html>";
HtmlParser htmlParser = new HtmlParser(new List<ITag>()
{ new MyList(), new MyInput() });
ITag tag = htmlParser.Parse(html);
MyList myList = tag.GetChildren()
.Where(x => x is MyList)
.FirstOrDefault().As<MyList>();
myList.ToString();
}
}
}
myList.ToString();
> "<myList class=\"breadcrumb m-0\" ><li class=\"breadcrumb-item\" ><a >Example 1</a></li><li class=\"breadcrumb-item\" ><a >Example 2</a></li><li class=\"breadcrumb-item\" ><a >Example 3</a></li></myList>"
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net5.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.