Content service

    Load, inspect, transform and export HTML through the awaited Content facade. Use it when your application owns document persistence, imports or generated messages.

    The facade is available as editor.Content; its interface can be replaced by an application implementation.

    Actual Avalonia HTML source editor showing a concise formatted document, syntax colours, line numbers and the Source mode controls.
    Actual Avalonia HTML source editor showing a concise formatted document, syntax colours, line numbers and the Source mode controls.

    Use it in your application

    The editor must be attached to a visible window before readiness can complete. Fresh exports apply a pending Source draft or report an apply failure; they do not silently return an older rendered document.

    All examples assume an editor named editor, already attached to a visible window, and an awaited editor.Ready. Keep operations asynchronous instead of blocking the UI thread.

    string body = await editor.Content.GetBodyHtmlAsync();
    string document = await editor.Content.GetDocumentHtmlAsync();
    

    Members

    These declarations come from the package's own XML documentation. Await operations and supply cancellation tokens where your application needs them.

    ChangeElementIdAsync Method

    Changes the identified element id as an undoable design operation.

    Syntax
    Task<EditorSnapshot> ChangeElementIdAsync(string oldId, string newId, CancellationToken cancellationToken = default)
    

    ClearContentAsync Method

    Clears body content while retaining the accepted document head.

    Syntax
    Task<EditorSnapshot> ClearContentAsync(CancellationToken cancellationToken = default)
    

    ElementExistsAsync Method

    Tests whether an element with the exact id exists in an accepted document export.

    Syntax
    Task<bool> ElementExistsAsync(string elementId, CancellationToken cancellationToken = default)
    

    EmbedLocalImagesAsBase64Async Method

    Embeds authorized local IMG sources as base64 in one Design undo operation. Source is a no-op; Preview/read-only reject mutation.

    Syntax
    Task EmbedLocalImagesAsBase64Async(CancellationToken cancellationToken = default)
    

    Honors BaseUrl and local-selection policy, validates raster bytes and never fetches remote images. Missing optional files retain their authored paths.

    GetAllImageSourcesAsync Method

    Returns decoded authored image sources from an accepted document export.

    Syntax
    Task<string[]> GetAllImageSourcesAsync(CancellationToken cancellationToken = default)
    

    GetAllLinkHrefsAsync Method

    Returns decoded authored link destinations from an accepted document export.

    Syntax
    Task<string[]> GetAllLinkHrefsAsync(CancellationToken cancellationToken = default)
    

    GetAttributeValueOfAnElementAsync Method

    Returns the decoded attribute value, or an empty string if the element or attribute is absent.

    Syntax
    Task<string> GetAttributeValueOfAnElementAsync(string elementId, string attributeName, CancellationToken cancellationToken = default)
    

    GetBodyClassNameAsync Method

    Returns the decoded authored body class attribute.

    Syntax
    Task<string> GetBodyClassNameAsync(CancellationToken cancellationToken = default)
    

    GetBodyHtmlAsync Method

    Exports body HTML, optionally converting it through the canonical shared normalizer to a well-formed XML fragment.

    Syntax
    Task<string> GetBodyHtmlAsync(bool getInXhtml, CancellationToken cancellationToken = default)
    

    Applies a dirty Source draft first. Invalid XML characters, unbound namespace prefixes and size/depth violations reject conversion without changing accepted content.

    Exports accepted body HTML with the captured URL spelling policy, applying a pending source draft first. URL preparation never changes accepted content or loads resources.

    Syntax
    Task<string> GetBodyHtmlAsync(CancellationToken cancellationToken = default)
    

    GetBodyStyleAsync Method

    Returns the decoded authored body style attribute.

    Syntax
    Task<string> GetBodyStyleAsync(CancellationToken cancellationToken = default)
    

    GetBodyTextAsync Method

    DOM textContent, whose UTF-16 positions are used by Selection.SelectTextAsync.

    Syntax
    Task<string> GetBodyTextAsync(CancellationToken cancellationToken = default)
    

    GetDocumentHtmlAsync Method

    Exports the complete document, optionally converting it to well-formed XML-compatible XHTML without an XML declaration.

    Syntax
    Task<string> GetDocumentHtmlAsync(bool getInXhtml, CancellationToken cancellationToken = default)
    

    Retains captured URL, doctype and generator policy. No external DTD or entity is resolved; an internal DTD subset is rejected.

    Exports the accepted complete document with the instance's doctype/generator policy, applying a pending source draft first. Read-only accepted exports are available; oversized or excessively nested output is rejected.

    Syntax
    Task<string> GetDocumentHtmlAsync(CancellationToken cancellationToken = default)
    

    GetDocumentXmlAsync Method

    Exports a complete well-formed XML document with a UTF-8 declaration after applying any pending Source draft.

    Syntax
    Task<string> GetDocumentXmlAsync(CancellationToken cancellationToken = default)
    

    Uses the same captured policy and canonical normalization as XHTML export, with bounded cancellable conversion and no resource access.

    GetEmailMessageWithLocalImagesEmbeddedAsync Method

    Builds an unsent email with formatted text, an HTML alternate view and validated local/embedded image linked resources.

    Syntax
    Task<System.Net.Mail.MailMessage> GetEmailMessageWithLocalImagesEmbeddedAsync(CancellationToken cancellationToken = default)
    

    Applies a pending Source draft first, performs no mail transport, and grants no implicit filesystem or network access. The caller owns and must dispose the returned message and its resource streams.

    GetFormattedPlainTextAsync Method

    Returns rendered plain text with layout breaks after applying a pending source draft.

    Syntax
    Task<string> GetFormattedPlainTextAsync(CancellationToken cancellationToken = default)
    

    GetInnerHtmlOfAnElementAsync Method

    Returns exported inner HTML, or an empty string if the exact element id is absent.

    Syntax
    Task<string> GetInnerHtmlOfAnElementAsync(string elementId, CancellationToken cancellationToken = default)
    

    GetOuterHtmlOfAnElementAsync Method

    Returns exported outer HTML, or an empty string if the exact element id is absent.

    Syntax
    Task<string> GetOuterHtmlOfAnElementAsync(string elementId, CancellationToken cancellationToken = default)
    

    InsertHeadElementAsync Method

    Inserts sanitized meta, title, or local style elements; scripts and external resource links are excluded.

    Syntax
    Task<EditorSnapshot> InsertHeadElementAsync(string tagName, string html, CancellationToken cancellationToken = default)
    

    InsertHorizontalRuleAsync Method

    Inserts a Design horizontal rule, or replaces the literal Source selection with the canonical <hr /> markup.

    Syntax
    Task InsertHorizontalRuleAsync(CancellationToken cancellationToken = default)
    

    InsertHtmlAsync Method

    Replaces the current selection with sanitized design HTML or literal source markup, resolving relative URLs through the shared BaseUrl policy.

    Syntax
    Task InsertHtmlAsync(string html, bool keepSelected = false, CancellationToken cancellationToken = default)
    

    InsertTextAsync Method

    Replaces the current selection with literal text in the visible Design document or Source draft; HTML-significant characters remain literal text.

    Syntax
    Task InsertTextAsync(string text, CancellationToken cancellationToken = default)
    

    MailMerge Property

    Canonical placeholder insertion and merge operations.

    Syntax
    MailMergeService MailMerge { get; }
    

    ReapplyDefaultBodyStyleAsync Method

    Deliberately reapplies acknowledged configured body font family, font-size CSS and text color after a later load.

    Syntax
    Task ReapplyDefaultBodyStyleAsync(CancellationToken cancellationToken = default)
    

    One guarded body-property operation preserves content and unrelated CSS. Source is a no-op. Ordinary document loads and recovery do not reapply defaults.

    RemoveHtmlElementByIdAsync Method

    Removes the identified element as an undoable design operation.

    Syntax
    Task<EditorSnapshot> RemoveHtmlElementByIdAsync(string id, CancellationToken cancellationToken = default)
    

    RemoveHtmlElementFromHeadByTagAsync Method

    Removes permitted metadata, title or local style elements from the document head.

    Syntax
    Task<EditorSnapshot> RemoveHtmlElementFromHeadByTagAsync(string tagName, CancellationToken cancellationToken = default)
    

    SetAttributeValueOfAnElementAsync Method

    Updates a validated attribute, removes it for null or blank values, and leaves a missing element unchanged.

    Syntax
    Task<EditorSnapshot> SetAttributeValueOfAnElementAsync(string elementId, string attributeName, string? value, CancellationToken cancellationToken = default)
    

    SetBodyClassNameAsync Method

    Sets the authored body class attribute.

    Syntax
    Task<EditorSnapshot> SetBodyClassNameAsync(string className, CancellationToken cancellationToken = default)
    

    SetBodyHtmlAsync Method

    Replaces sanitized body content while retaining the accepted document head.

    Syntax
    Task<EditorSnapshot> SetBodyHtmlAsync(string html, CancellationToken cancellationToken = default)
    

    SetBodyStyleAsync Method

    Sets authored body style after resource and active-content validation.

    Syntax
    Task<EditorSnapshot> SetBodyStyleAsync(string styleValue, CancellationToken cancellationToken = default)
    

    SetBodyTextAsync Method

    Replaces body content with literal text as an undoable design operation.

    Syntax
    Task<EditorSnapshot> SetBodyTextAsync(string text, CancellationToken cancellationToken = default)
    

    SetDefaultBodyFontFamilyAsync Method

    Configures authored body font-family CSS without inserting caret spans.

    Syntax
    Task SetDefaultBodyFontFamilyAsync(string fontFamily, CancellationToken cancellationToken = default)
    

    Before Ready, buffers the request for guarded startup. In editable Design, awaits acknowledgment and retains the default for explicit reapply after later loads. Source is a no-op and does not commit its draft.

    SetDefaultBodyFontSizeAsync Method

    Configures authored body font-size CSS, retaining its supplied unit and precision for explicit reapply.

    Syntax
    Task SetDefaultBodyFontSizeAsync(string cssFontSize, CancellationToken cancellationToken = default)
    

    Uses the same startup, Design and Source semantics as SetDefaultBodyFontFamilyAsync. The point-valued native convenience property is a derived view; it does not replace the configured CSS value.

    SetDefaultBodyForeColorAsync Method

    Configures authored body text color CSS and retains the acknowledged default for explicit reapply.

    Syntax
    Task SetDefaultBodyForeColorAsync(string cssColor, CancellationToken cancellationToken = default)
    

    Uses the same startup, Design and Source semantics as SetDefaultBodyFontFamilyAsync.

    SetDocumentHtmlAsync Method

    Replaces the sanitized complete document and resets document history.

    Syntax
    Task<EditorSnapshot> SetDocumentHtmlAsync(string html, CancellationToken cancellationToken = default)
    

    TableAuthoringService Property

    Actual table authoring operations for this document service.

    Syntax
    TableAuthoringService TableAuthoringService { get; }
    

    UpdateInnerHtmlOfAnElementAsync Method

    Replaces the identified element content with sanitized HTML as an undoable design operation.

    Syntax
    Task<EditorSnapshot> UpdateInnerHtmlOfAnElementAsync(string elementId, string newHtml, CancellationToken cancellationToken = default)
    

    Related guides

    Last updated on Oct 30, 2026

    Put this into practice.

    Start with the C# Avalonia sample. Restore this edition's package and install the native browser prerequisites before running it.

    Prefer a guided look? Book a free live demo with our engineers - live on Zoom or Teams, never a chatbot.