Dans.Html.Library
25.2816.3809
See the version list below for details.
dotnet add package Dans.Html.Library --version 25.2816.3809
NuGet\Install-Package Dans.Html.Library -Version 25.2816.3809
<PackageReference Include="Dans.Html.Library" Version="25.2816.3809" />
<PackageVersion Include="Dans.Html.Library" Version="25.2816.3809" />
<PackageReference Include="Dans.Html.Library" />
paket add Dans.Html.Library --version 25.2816.3809
#r "nuget: Dans.Html.Library, 25.2816.3809"
#:package Dans.Html.Library@25.2816.3809
#addin nuget:?package=Dans.Html.Library&version=25.2816.3809
#tool nuget:?package=Dans.Html.Library&version=25.2816.3809
Dan's HTML Library
An easy-to-use .NET function library for parsing and directly handling HTML and similar markup languages that use angle-bracketed tags. It supports structures where opening tags may contain name="value" property assignments, such as XML.
Following are some basic examples.
Programmatically Creating an HTML Document from Scratch
This how-to shows you how to build a basic HTML document using the HtmlDocument
and HtmlNodeItem
classes. This is useful when you want to programmatically generate HTML content, such as for templating, automation, or internal tools.
Goal
Create an in-memory HTML document structure that resembles:
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Welcome!</h1>
<p>This document was created using <i>Dans.Html.Library</i>.</p>
</body>
</html>
Step-by-Step Example (C#)
using Html;
public class HtmlBuilderExample
{
//*-----------------------------------------------------------------------*
//* CreateHtmlDocumentProgrammatically *
//*-----------------------------------------------------------------------*
/// <summary>
/// Create an HTML document programmatically from scratch.
/// </summary>
private static void CreateHtmlDocumentProgrammatically()
{
HtmlDocument doc = new HtmlDocument();
HtmlNodeItem docType = new HtmlNodeItem("!DOCTYPE");
HtmlNodeItem html = new HtmlNodeItem("html");
// Set the document type to HTML.
docType.Attributes.Add(new HtmlAttributeItem()
{
Name = "html",
Presence = true
});
// Create the head node with a title.
HtmlNodeItem head = new HtmlNodeItem("head");
HtmlNodeItem title = new HtmlNodeItem("title", "Hello World!");
head.Nodes.Add(title);
html.Nodes.Add(head);
// Create the body with some basic content.
HtmlNodeItem body = new HtmlNodeItem("body");
body.Nodes.Add("h1", "Welcome!");
body.Nodes.Add("<p>" +
"This document was created using <i>Dans.Html.Library</i>.</p>",
true);
html.Nodes.Add(body);
// Assemble the document.
doc.Nodes.Add(docType);
doc.Nodes.Add(html);
Console.WriteLine("The programmatic document is:");
Console.WriteLine(doc.Html);
}
//*-----------------------------------------------------------------------*
}
Programmatically Create an HTML Snippet
Creating a small HTML snippet is as easy as creating a new HtmlNodeItem and populating it. No further configuration is needed.
Once created in memory, it can be merged into other trees, serialized, or processed in any way you like.
Any snippet can be rendered to HTML by reading the Html property, the same way you would do with a full HtmlDocument.
Goal
Create an in-memory HTML node that resembles:
<div class="paragraph-list">
<p>This is one of the paragraphs.</p>
<p>This is the second paragraph.</p>
<p>This is the last paragraph.</p>
</div>
Step-by-Step Example (C#)
using Html;
public class HtmlBuilderExample
{
//*-----------------------------------------------------------------------*
//* CreatePartialHtmlDocumentProgrammatically *
//*-----------------------------------------------------------------------*
/// <summary>
/// Create a partial HTML document programmatically from scratch.
/// </summary>
/// <remarks>
/// In this example, a stand-alone, self-renderable snippet is created.
/// </remarks>
private static void CreatePartialHtmlDocumentProgrammatically()
{
HtmlNodeItem div = new HtmlNodeItem("div");
div.Attributes.AddClass("paragraph-list");
div.Nodes.Add("p", "This is one of the paragraphs.");
div.Nodes.Add("p", "This is the second paragraph.");
div.Nodes.Add("p", "This is the last paragraph.");
Console.WriteLine("The snippet is:");
Console.WriteLine(div.Html);
Console.WriteLine("");
}
//*-----------------------------------------------------------------------*
}
Parse HTML Text
To parse an HTML document, you will generally be using the HtmlDocument.Parse method, where there are overloads for various conditions.
A usual case follows in the example.
Step-by-Step Example (C#)
using Html;
public class HtmlBuilderExample
{
//*-----------------------------------------------------------------------*
//* CreateHtmlDocumentFromContent *
//*-----------------------------------------------------------------------*
/// <summary>
/// Create an HTML document from string content.
/// </summary>
private static void CreateHtmlDocumentFromContent()
{
StringBuilder builder = new StringBuilder();
Match match = null;
builder.Append(@"<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Welcome!</h1>
<p>This document was created using the Data.Html library.</p>
</body>
</html>");
HtmlDocument doc = HtmlDocument.Parse(builder.ToString(), true, false);
// Trim the text of all of the nodes.
List<HtmlNodeItem> flatNodesList =
doc.Nodes.FindMatches(x => x.Text?.Length >= 0);
foreach(HtmlNodeItem nodeItem in flatNodesList)
{
nodeItem.Text = nodeItem.Text.Trim();
nodeItem.TrailingText = nodeItem.TrailingText.Trim();
}
Console.WriteLine("The document from content is:");
Console.WriteLine(doc.Html);
}
//*-----------------------------------------------------------------------*
}
Updates
Version | Description |
---|---|
25.2816.3809 | A PreserveSpace option has been added to the HtmlDocument that preserves all whitespace during parsing and rendering when set; Each HtmlNodeItem now has a TrailingText property that contains the information between the end of this node and the beginning of the next. |
25.2806.4417 | An element name is now allowed to contain hyphens, underscores, and digits after the first character, which must be a letter; HtmlNodeItem.InnerHtml now returns the combined content of the node's Text and Nodes.Html properties. |
25.2711.4233 | Any inner text following a comment block is now parsed into the object model as a blank sibling to that comment with its Text property set; an issue where the trailing text at the end of a child node was being placed on a new line has been resolved; instance-level GetValue(string attributeName) function has been added to the HtmlAttributeCollection class, to match the GetStyle(string styleName) function. |
25.2515.4053 | A bug was fixed that had been introduced in version 25.2515.3752. The error was that no HTML nodes had closing tags. |
25.2515.3752 | The static Singles property has been moved from HtmlDocument to HtmlUtil, and is now initialized at startup; HtmlDocument.Singles has been depreciated; static read-only HtmlUtil.HtmlNodeTypes property has been added to return all of the currently known HTML node types. |
25.2513.3949 | When child nodes are added to an HtmlNodeItem, the parent node's SelfClosing property is now reset automatically. |
25.2504.3941 | Change in assembly name; single element node tags have been made case-insensitive; HtmlNodeItem(string nodeType, string text) constructor overload has been added to accept node type and text; HtmlNodeItem.ToString() now returns the item's node type to aid in debugging. |
25.2502.4550 | Initial public release. |
More Information
For more information, please see the GitHub project: danielanywhere/Html
Full API documentation is available at this library's GitHub User Page.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Dans.Html.Library:
Package | Downloads |
---|---|
Dans.SvgPlotting.Library
Dan's SVG Plotting Library. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
25.2910.4125 | 69 | 9/10/2025 |
25.2904.4408 | 143 | 9/4/2025 |
25.2822.3845 | 71 | 8/22/2025 |
25.2819.4048 | 133 | 8/19/2025 |
25.2819.3939 | 125 | 8/19/2025 |
25.2816.3809 | 55 | 8/16/2025 |
25.2806.4417 | 213 | 8/6/2025 |
25.2711.4233 | 114 | 7/11/2025 |
25.2515.4053 | 241 | 5/15/2025 |
25.2513.3949 | 234 | 5/13/2025 |
25.2504.3941 | 153 | 5/4/2025 |
25.2502.4550 | 86 | 5/2/2025 |