Skip to content

bookgen.api.htmlbuilder

webmaster442 edited this page Jul 19, 2026 · 2 revisions

< Back


HtmlBuilder

Namespace: BookGen.Api

A builder class for generating HTML content in a fluent manner.

public sealed class HtmlBuilder

Inheritance ObjectHtmlBuilder
Attributes NullableContextAttribute, NullableAttribute

Remarks:

Initializes a new instance of the HtmlBuilder class with the specified initial capacity.

Properties

LastUnclosedTag

Gets the name of the last unclosed HTML tag, or null if there are no open tags.

public string? LastUnclosedTag { get; }

Property Value

String

OpenTags

Gets an enumerable of all currently open HTML tags in reverse order (from the most recently opened to the least).

public IEnumerable<string> OpenTags { get; }

Property Value

IEnumerable<String>

Constructors

HtmlBuilder(Int32)

A builder class for generating HTML content in a fluent manner.

public HtmlBuilder(int size)

Parameters

size Int32
The initial capacity of the underlying StringBuilder.

Remarks:

Initializes a new instance of the HtmlBuilder class with the specified initial capacity.

Methods

Create(Int32)

Creates a new instance of HtmlBuilder with the specified initial capacity.

public static HtmlBuilder Create(int size = 4096)

Parameters

size Int32
The initial capacity of the underlying StringBuilder.

Returns

HtmlBuilder
A new instance of HtmlBuilder.

Element(String, Action<HtmlBuilder>)

Creates an HTML element with the specified tag name and optional content.

public HtmlBuilder Element(string tag, Action<HtmlBuilder>? content = null)

Parameters

tag String
The name of the HTML tag.

content Action<HtmlBuilder>?
An optional action to generate the content of the element.

Returns

HtmlBuilder
The current HtmlBuilder instance.

Element(String, Action<TagBuilder>, Action<HtmlBuilder>)

Creates an HTML element with the specified tag name, attributes, and optional content.

public HtmlBuilder Element(string tag, Action<TagBuilder> configure, Action<HtmlBuilder>? content = null)

Parameters

tag String
The name of the HTML tag.

configure Action<TagBuilder>
An action to configure the attributes of the tag.

content Action<HtmlBuilder>?
An optional action to generate the content of the element.

Returns

HtmlBuilder
The current HtmlBuilder instance.

CloseOpenTag()

Closes the most recently opened HTML element.

public HtmlBuilder CloseOpenTag()

Returns

HtmlBuilder
The current HtmlBuilder instance.

Exceptions

InvalidOperationException
Thrown if there are no open elements to close.

Text(String)

Appends text content to the current HTML element, encoding it to prevent XSS attacks.

public HtmlBuilder Text(string text)

Parameters

text String
The text content to append.

Returns

HtmlBuilder
The current HtmlBuilder instance.

Raw(String)

Appends raw HTML content to the current HTML element without encoding.

public HtmlBuilder Raw(string html)

Parameters

html String
The raw HTML content to append.

Returns

HtmlBuilder
The current HtmlBuilder instance.

SelfClosing(String, Action<TagBuilder>)

Creates a self-closing HTML element with the specified tag name and optional attributes.

public HtmlBuilder SelfClosing(string tag, Action<TagBuilder>? configure = null)

Parameters

tag String
The name of the HTML tag.

configure Action<TagBuilder>?
An optional action to configure the attributes of the tag.

Returns

HtmlBuilder
The current HtmlBuilder instance.

ToString()

Returns the generated HTML content as a string. Throws an exception if there are unclosed HTML elements.

public override string ToString()

Returns

String
The generated HTML content as a string.

Exceptions

InvalidOperationException
Thrown if there are unclosed HTML elements.


< Back

Clone this wiki locally