Convert HTML to Markdown in C#

To convert HTML to Markdown in C#, use Converter.ConvertHTML() with an HTML source, MarkdownSaveOptions, and an output path. Aspose.HTML for .NET can produce standard Markdown, Git-style Markdown, or Markdown output limited to selected HTML features.

This guide shows the core HTML to Markdown workflows for C# applications: basic conversion, custom MarkdownSaveOptions, Git-style Markdown output, and inline HTML handling. Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.

Markdown is a plain-text markup format commonly used for documentation, README files, static-site content, and developer publishing workflows. HTML to Markdown conversion is useful when an application needs to migrate existing HTML content into a readable text format, prepare documentation sources, or normalize generated HTML for further editing.

Convert HTML to Markdown in C#

The static Converter class provides a direct way to convert HTML content to Markdown. The example below uses ConvertHTML() with HTML input and writes the result to a Markdown file.

 1// Convert HTML to Markdown in C#
 2
 3// Prepare HTML code and save it to a file
 4string code = "<h1>Header 1</h1>" +
 5              "<h2>Header 2</h2>" +
 6              "<p>Convert HTML to Markdown</p>";
 7File.WriteAllText("convert.html", code);
 8
 9// Call ConvertHTML() method to convert HTML to Markdown
10Converter.ConvertHTML("convert.html", new MarkdownSaveOptions(), Path.Combine(OutputDir, "convert.md"));

Use this workflow when default Markdown output is enough and the source HTML does not need special filtering rules. For more controlled output, configure MarkdownSaveOptions as shown below.

Short HTML to Markdown Recipes

Customize HTML to Markdown with MarkdownSaveOptions

Use MarkdownSaveOptions when your application needs predictable Markdown output instead of a default conversion. The most important setting is Features, which controls which HTML elements are converted into Markdown syntax.

OptionUse it to control
DefaultA predefined option set compatible with default Markdown documentation.
FeaturesA flag set that controls which HTML elements are converted to Markdown.
FormatterMarkdown formatting style used in the generated output.
GitA predefined option set compatible with Git-style Markdown.
ResourceHandlingOptionsResource handling behavior during conversion.

The workflow is:

  1. Load the HTML file with an HTMLDocument constructor.
  2. Create a MarkdownSaveOptions instance.
  3. Configure the required options, such as Features.
  4. Call Converter.ConvertHTML(document, options, savePath) to write the Markdown file.

The following example processes only links and paragraphs. Other HTML elements remain unchanged in the output.

 1// Convert selective HTML tags to Markdown using C#
 2
 3// Prepare a path for converted file saving 
 4string savePath = Path.Combine(OutputDir, "options-output.md");
 5
 6// Prepare HTML code and save it to the file
 7string code = "<h1>Header 1</h1>" +
 8              "<h2>Header 2</h2>" +
 9              "<p>Hello, World!!</p>" +
10              "<a href='aspose.com'>aspose</a>";
11File.WriteAllText(Path.Combine(OutputDir, "options.html"), code);
12
13// Create an instance of SaveOptions and set up the rule: 
14// - only <a> and <p> elements will be converted to Markdown
15MarkdownSaveOptions options = new MarkdownSaveOptions();
16options.Features = MarkdownFeatures.Link | MarkdownFeatures.AutomaticParagraph;
17
18// Call the ConvertHTML() method to convert the HTML to Markdown
19Converter.ConvertHTML(Path.Combine(OutputDir, "options.html"), options, savePath);

For deeper converter settings, see Fine-Tuning Converters.

Generate Git-Style Markdown

You can define your own conversion rules or use predefined templates. For example, the Git option creates Markdown compatible with Git-style Markdown syntax.

 1// Convert HTML to Markdown in C# using Git syntax
 2
 3// Prepare a path for converted file saving 
 4string savePath = Path.Combine(OutputDir, "output-git.md");
 5
 6// Prepare HTML code and save it to the file
 7string code = "<h1>Header 1</h1>" +
 8              "<h2>Header 2</h2>" +
 9              "<p>Hello, World!!</p>";
10File.WriteAllText(Path.Combine(OutputDir, "document.html"), code);
11
12// Call ConvertHTML() method to convert HTML to Markdown
13Converter.ConvertHTML(Path.Combine(OutputDir, "document.html"), MarkdownSaveOptions.Git, savePath);

Use Git-style Markdown when the output will be stored in repositories, documentation portals, or systems that support Git-flavored Markdown extensions.

Preserve Inline HTML During Conversion

Not every HTML element has a direct Markdown equivalent. Markdown processors usually allow selected HTML blocks to remain in the document and be rendered as HTML. This behavior is useful for elements that must keep their original structure.

To preserve an element as inline HTML, mark it with the markdown attribute and the inline value. The example below keeps the content of a <div> element as HTML instead of converting it to Markdown.

 1// Convert inline HTML elements to Markdown using C#
 2
 3// Prepare a path for converted file saving 
 4string savePath = Path.Combine(OutputDir, "inline-html.md");
 5
 6// Prepare HTML code and save it to the file
 7string code = "text<div markdown='inline'><code>text</code></div>";
 8File.WriteAllText(Path.Combine(OutputDir, "inline.html"), code);
 9
10// Call ConvertHTML() method to convert HTML to Markdown
11Converter.ConvertHTML(Path.Combine(OutputDir, "inline.html"), new MarkdownSaveOptions(), savePath);
12
13// Output file will contain: text\r\n<div markdown="inline"><code>text</code></div>

The list of elements that support inline HTML behavior depends on the Markdown processor used after conversion. The original Markdown specification and Git-style Markdown support different sets of HTML tags, so validate the generated .md file in the target publishing system.

HTML to Markdown Conversion Limitations

Markdown is intentionally lightweight, so some HTML elements cannot be represented as native Markdown syntax. Elements such as STYLE, SCRIPT, LINK, and EMBED can be discarded or preserved as HTML depending on the selected options and processor behavior.

Feature Nesting

Markdown supports many formatting features, but not every feature can be nested inside another one. For example, list elements inside table elements may not be converted to native Markdown. Each feature is a member of the MarkdownFeatures enumeration.

Parent featureFeatures which can be processed inside
HeaderLink, Emphasis, Strong, InlineCode, Image, Strikethrough, Video
BlockquoteAny
ListAutomaticParagraph, Link, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough, Video, TaskList, List
LinkEmphasis, Strong, InlineCode, Image, LineBreak, Strikethrough
AutomaticParagraphLink, Emphasis, Strong, InlineCode, Image, LineBreak, Strikethrough
StrikethroughLink, Emphasis, Strong, InlineCode, Image, LineBreak
TableVideo, Strikethrough, Image, InlineCode, Emphasis, Strong, Link
EmphasisLink, InlineCode, Image, LineBreak, Strikethrough, Video
StrongLink, InlineCode, Image, LineBreak, Strikethrough, Video

Related Markdown Workflows

Related HTML Conversion Guides

Other Platforms

Try Online HTML to Markdown Conversion

                
            

Use the online HTML to MD Converter for quick manual conversion. Use Aspose.HTML for .NET when HTML to Markdown conversion must run inside a C# application, documentation pipeline, or content migration workflow.

HTML to MD Converter