HtmlMarkupWriter Class

Writes the markup of a node. The default implementation is the HTML fragment serialization algorithm (https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments); a derived class customizes the output by overriding the method of each kind of node.

public class HtmlMarkupWriter

Remarks

The tree is walked iteratively, so deep trees do not overflow the stack. An instance writes one node: create a new writer for each call (see HtmlFormatter.CreateWriter).

Constructors

NameDescription
HtmlMarkupWriter(TextWriter)Creates a writer that emits HTML markup to the given output.

HtmlMarkupWriter(TextWriter)

HtmlMarkupWriter.cs

Creates a writer that emits HTML markup to the given output.

public HtmlMarkupWriter(TextWriter output)

Parameters

output TextWriter

Properties

NameDescription
DepthThe number of elements, between the root and the node being written, whose start tag is written: 0 for the root element and for the children of a root document; the children of an element are at the depth of the element plus 1.
OutputThe writer the markup is written to.
RootThe node passed to HtmlMarkupWriter.Write.
StyleAttributeFormatterThe formatter of the value of the style attributes (see HtmlMarkupWriter.WriteAttributes), or null to write it as is. HtmlFormatter.Format sets it to HtmlFormatter.StyleAttributeFormatter when it is null.
StyleElementFormatterThe formatter of the CSS text of the style elements (see HtmlMarkupWriter.WriteRawText), or null to write it as is. HtmlFormatter.Format sets it to HtmlFormatter.StyleElementFormatter when it is null.

The number of elements, between the root and the node being written, whose start tag is written: 0 for the root element and for the children of a root document; the children of an element are at the depth of the element plus 1.

protected int Depth { get; }

Property value

int

The writer the markup is written to.

protected TextWriter Output { get; }

Property value

TextWriter

The node passed to HtmlMarkupWriter.Write.

protected Node? Root { get; }

Property value

Node?

StyleAttributeFormatter

HtmlMarkupWriter.cs

The formatter of the value of the style attributes (see HtmlMarkupWriter.WriteAttributes), or null to write it as is. HtmlFormatter.Format sets it to HtmlFormatter.StyleAttributeFormatter when it is null.

public CssFormatter? StyleAttributeFormatter { get; set; }

Property value

CssFormatter?

StyleElementFormatter

HtmlMarkupWriter.cs

The formatter of the CSS text of the style elements (see HtmlMarkupWriter.WriteRawText), or null to write it as is. HtmlFormatter.Format sets it to HtmlFormatter.StyleElementFormatter when it is null.

public CssFormatter? StyleElementFormatter { get; set; }

Property value

CssFormatter?

Methods

NameDescription
ShouldWrite(Node)Returns whether node and its descendants are written. Returns true by default.
Write(Node)Writes node: an element with its start tag, its descendants and its end tag (as the outerHTML getter), the children of a document or a document fragment.
WriteAttribute(Element, string, string)Writes an attribute of the element: a space, then name="value" with the value escaped.
WriteAttributes(Element)Calls HtmlMarkupWriter.WriteAttribute for each attribute of the element, preceded by the is value of the element when it has one but no is attribute. The value of the style attribute is formatted by HtmlMarkupWriter.StyleAttributeFormatter when it is set.
WriteComment(Comment)Writes <!--data-->.
WriteDocumentType(DocumentType)Writes <!DOCTYPE name>: as defined by the serialization algorithm, the public and system identifiers are not written.
WriteEndTag(Element)Writes the end tag of the element. It is not called for the void elements (HtmlFormatter.IsVoidElement).
WriteProcessingInstruction(ProcessingInstruction)Writes <?target data?>.
WriteRawText(Text)Writes a text node whose parent has raw text content (HtmlFormatter.HasRawTextContent): as is, or for a style element, the text of its style sheet formatted by HtmlMarkupWriter.StyleElementFormatter in place of its first text node (its other text nodes are not written).
WriteStartTag(Element)Writes the start tag of the element: <, the tag name, the attributes (HtmlMarkupWriter.WriteAttributes) and >.
WriteText(Text)Writes a text node: with HtmlMarkupWriter.WriteRawText when its parent has raw text content (HtmlFormatter.HasRawTextContent), escaped otherwise.

ShouldWrite(Node)

HtmlMarkupWriter.cs

Returns whether node and its descendants are written. Returns true by default.

protected virtual bool ShouldWrite(Node node)

Parameters

node Node

Returns

bool

Writes node: an element with its start tag, its descendants and its end tag (as the outerHTML getter), the children of a document or a document fragment.

public virtual void Write(Node node)

Parameters

node Node

WriteAttribute(Element, string, string)

HtmlMarkupWriter.cs

Writes an attribute of the element: a space, then name="value" with the value escaped.

protected virtual void WriteAttribute(Element element, string serializedName, string value)

Parameters

element Element
The element of the attribute.
serializedName string
The name of the attribute, prefixed as defined by HtmlFormatter.GetSerializedAttributeName.
value string
The value of the attribute.

WriteAttributes(Element)

HtmlMarkupWriter.cs

Calls HtmlMarkupWriter.WriteAttribute for each attribute of the element, preceded by the is value of the element when it has one but no is attribute. The value of the style attribute is formatted by HtmlMarkupWriter.StyleAttributeFormatter when it is set.

protected void WriteAttributes(Element element)

Parameters

element Element

WriteComment(Comment)

HtmlMarkupWriter.cs

Writes <!--data-->.

protected virtual void WriteComment(Comment comment)

Parameters

comment Comment

WriteDocumentType(DocumentType)

HtmlMarkupWriter.cs

Writes <!DOCTYPE name>: as defined by the serialization algorithm, the public and system identifiers are not written.

protected virtual void WriteDocumentType(DocumentType documentType)

Parameters

documentType DocumentType

WriteEndTag(Element)

HtmlMarkupWriter.cs

Writes the end tag of the element. It is not called for the void elements (HtmlFormatter.IsVoidElement).

protected virtual void WriteEndTag(Element element)

Parameters

element Element

WriteProcessingInstruction(ProcessingInstruction)

HtmlMarkupWriter.cs

Writes <?target data?>.

protected virtual void WriteProcessingInstruction(ProcessingInstruction processingInstruction)

Parameters

processingInstruction ProcessingInstruction

WriteRawText(Text)

HtmlMarkupWriter.cs

Writes a text node whose parent has raw text content (HtmlFormatter.HasRawTextContent): as is, or for a style element, the text of its style sheet formatted by HtmlMarkupWriter.StyleElementFormatter in place of its first text node (its other text nodes are not written).

protected virtual void WriteRawText(Text text)

Parameters

text Text

WriteStartTag(Element)

HtmlMarkupWriter.cs

Writes the start tag of the element: <, the tag name, the attributes (HtmlMarkupWriter.WriteAttributes) and >.

protected virtual void WriteStartTag(Element element)

Parameters

element Element

WriteText(Text)

HtmlMarkupWriter.cs

Writes a text node: with HtmlMarkupWriter.WriteRawText when its parent has raw text content (HtmlFormatter.HasRawTextContent), escaped otherwise.

protected virtual void WriteText(Text text)

Parameters

text Text