Load and save HTML

    Load HTML into an Avalonia editor, let users revise it visually, then save the current result through the C# content API. Choose a body fragment for an application field or a complete HTML document when head metadata and CSS must travel with it.

    Edited HTML reopened in Avalonia HTML Editor with its approval sentence, headings and table preserved.
    Edited HTML reopened in Avalonia HTML Editor with its approval sentence, headings and table preserved.
    await editor.Ready;
    await editor.Content.SetBodyHtmlAsync("<p>Hello</p>");
    string fragment = await editor.Content.GetBodyHtmlAsync();
    string fullDocument = await editor.Content.GetDocumentHtmlAsync();
    

    Use SetDocumentHtmlAsync when replacing a complete document. Setting body HTML preserves the accepted document head. Content export applies pending source changes; if newer edits remain or an operation fails, keep the source draft visible and handle the error before saving.

    Bound properties and awaited results

    BodyHtml and DocumentHtml are bindable properties reflecting accepted editor content. Use an awaited setter or getter when the next application operation depends on completion. The native Border.Child property owns the visual tree; Content is the replaceable awaited authoring facade.

    Save accepted content

    A body replacement retains supported head metadata and styles. A full document replacement establishes a new document identity. Private raster display transport is removed from exported HTML; explicitly embedded image data belongs to the authored document and remains in exports.

    XHTML and XML export

    The XHTML overloads convert through the shared normalizer to well-formed XML-compatible output. The XML method adds a UTF-8 declaration. They apply a pending Source draft first and retain the current URL, doctype and generator policy. Invalid XML characters, unbound namespace prefixes, internal DTD subsets and excessive size or depth report an error; they never replace the accepted document.

    string xhtmlFragment = await editor.Content.GetBodyHtmlAsync(true);
    string xhtmlDocument = await editor.Content.GetDocumentHtmlAsync(true);
    string xmlDocument = await editor.Content.GetDocumentXmlAsync();
    

    Last updated on Nov 5, 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.