DV8.Html 2.21.0

dotnet add package DV8.Html --version 2.21.0
                    
NuGet\Install-Package DV8.Html -Version 2.21.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="DV8.Html" Version="2.21.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DV8.Html" Version="2.21.0" />
                    
Directory.Packages.props
<PackageReference Include="DV8.Html" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DV8.Html --version 2.21.0
                    
#r "nuget: DV8.Html, 2.21.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.
#:package DV8.Html@2.21.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DV8.Html&version=2.21.0
                    
Install as a Cake Addin
#tool nuget:?package=DV8.Html&version=2.21.0
                    
Install as a Cake Tool

DV8.Html

C# package with a HTML DSL and support for generating HTML elements and serializing object graphs to HTML

This project is a dead simple and dependency free package to work with HTML elements from C# code.

In addition, there is support for serializing objects and graphs of objects to HTML.

Lots of elements and attributes are implemented, and you can generate missing elements/attributes at run time by specifying element/attribute names.

Writing out non-standard / non-safe HTML code is also supported.

Various helper methods are available to make it easy to work with attributes and elements.

See the test classes for more info.

Requirements/Installation/Usage

Requirements: .Net Core 6.0 or later.

Dependencies: None.

Nuget link: https://www.nuget.org/packages/DV8.Html/

Usage:

dotnet add package DV8.Html

Using the DSL-like syntax for generating HTML

   using static DV8.Html.Prefixes.Underscore;
    ...
   var fruits = new[] { "Apple", "Banana", "Cherry" };
   var html =
        _<Html>(
            _<Head>(
                _<Title>("Hello, World!")
            ),
            _<Body>(
                _<H1>("Hello, World!"),
                _<P>(
                    _("This is a paragraph with <>. "), // Becomes plain text, not an element. Text is escaped. 
                    _<Ul>(
                        fruits.Select(_<Li>)
                    )
                ),
                _UNSAFE("This will not be <b>escaped</b>") // Allows any HTML, don't use this with untrusted content. 
            )
        );
    var act = html.ToHtml();
    var exp = @"
    <!DOCTYPE html><html>
    <head><title>Hello, World!</title></head>
    <body><h1>Hello, World!</h1><p>This is a paragraph with &lt;&gt;. <ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul></p>
    This will not be <b>escaped</b>
    </body></html>";

    // "Canonical" strips linebreaks, whitespace between elements, and uses ' instead of " as attribute delimiter.
    Assert.AreEqual(exp.Canonical(), act).Canonical();

Using plain C#

var fruits = new[] { "Apple", "Banana", "Cherry" }
    .Select( f => new A( $"https://fruits.org/{f}));
var ul = new Ul(fruits)
    .WithClass("the-fruits");
ul.Attributes["my-attribute"] = "my-value";
ul.Properties["my-property"] = myFruitCollectionObject;
var p = new P("This is a paragraph with <>. ", ul);

var ulHtml = ul.ToHtml()

Generating XML / XHTML

Use ToXml instead of ToHtml if you want the output to be correct XML. This uses the .Net XmlWriter class and is probably safer and faster, however it will always close elements, so an input becomes <input ... /> instead of <input ...>. In addition, it will use " instead of ' as attribute delimiter.

Serialization

Example code for serializing objects to HTML (recursive to max 3 levels into properties)

var ser = HtmlSerializerRegistry.AddDefaults(new HtmlSerializerRegistry()); 
var elements = HtmlSerializer.Serialize(myListOrCustomObjectOrWhatever, 3);

This serializer can also be added as a HtmlOutputFormatter in Asp.Net, easily making all your JSON-APIs available as straight, human-readable HTML.

Semantic HTML / microformats / S

Documentation TBA 😃

Contributing

In the very unlikely event that anybody actually is interested in this project: Let me know (starring it on github is enough) and I'll improve documentation and samples 😃 Issues and pull requests are also welcome.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

    • No dependencies.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on DV8.Html:

Package Downloads
Nodes.API

Models and services for Nodes API

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.21.0 6,598 2/3/2025
2.20.0 193 1/31/2025
2.19.0 6,623 10/24/2023
2.18.0 218 10/24/2023
2.16.0 268 10/20/2023
2.15.0 218 10/20/2023
2.14.0 252 10/19/2023
2.13.0 533 10/8/2023
2.12.0 218 10/4/2023
2.11.0 220 10/2/2023
2.10.0 511 10/2/2023
2.8.0 241 10/2/2023
2.7.0 232 10/2/2023
2.6.0 219 10/2/2023
2.5.0 247 10/2/2023
2.4.0 196 10/1/2023
2.3.0 255 10/1/2023
2.2.0 288 10/1/2023
2.1.0 214 10/1/2023
2.0.0 220 10/1/2023
Loading failed