Convert Markdown to HTML – C#

Markdown is a markup language with a plain-text-formatting syntax. Markdown is often used as a format for documentation and readme files since it allows writing in an easy-to-read and easy-to-write style. Its design allows it to be easily converted to many output formats, but initially, it was created to convert only to HTML. Using the Aspose.HTML class library in your C# application, you can easily convert Markdown into an HTML file with just a few lines of code!

This article provides information on how to convert Markdown to HTML using the Aspose.HTML API. You will learn about the supported Markdown to HTML conversion scenarios and consider C# examples to illustrate them. Also, you can try an Online Markdown Converter to test the Aspose.HTML API functionality and convert Markdown on the fly.

Online Markdown Converter

You can convert Markdown to HTML with Aspose.HTML for .NET API in real time. Please load a Markdown file from the local file system and run the example. The save options are set by default. You will immediately receive the conversion result as a separate file.

                
            

If you want to convert Markdown to HTML programmatically, please see the following C# code examples.

Example 1. Create a source MD file from code and convert Markdown to HTML

You can convert Markdown to HTML format using C# and other .NET programming languages. Aspose.HTML provides ConvertMarkdown() methods as an understandable and straightforward way to perform Markdown to HTML conversion. If your case is to create a Markdown document from a user string directly in your code and save it to a file, the following example could help you:

  1. Prepare a source Markdown document. In the example, we create a Markdown file from code.
  2. Prepare a path for converted file saving.
  3. Use the ConvertMarkdown() method of the Converter class to save Markdown as an HTML file. You need to pass the sourcePath and savePath to the ConvertMarkdown() method for Markdown to HTML conversion.
 1using System.IO;
 2using Aspose.Html.Converters;
 3...
 4    // Prepare a path to a source Markdown file
 5    string sourcePath = Path.Combine(OutputDir, "document.md");
 6
 7    // Prepare a simple Markdown example
 8    var code = "### Hello, World!" +
 9               "\r\n" +
10               "[visit applications](https://products.aspose.app/html/applications)";
11    // Create a Markdown file
12    File.WriteAllText(sourcePath, code);
13
14    // Prepare a path for converted file saving 
15    string savePath = Path.Combine(OutputDir, "document-output.html");
16
17    // Convert Markdown to HTML document
18    Converter.ConvertMarkdown(sourcePath, savePath);

You can download the complete examples and data files from GitHub.

Example 2. Convert an existing Markdown file to HTML

If your case is to convert an existing Markdown document from a local file system, the following example could help you. You need to follow a few steps:

  1. Open an existing Markdown document. In the example, we load a Markdown file from a local file system ( nature.md).
  2. Prepare a path for converted file saving.
  3. Use the ConvertMarkdown() method of the Converter class to save Markdown as an HTML file. You need to pass the sourcePath and savePath to the ConvertMarkdown() method for Markdown to HTML conversion.

The following code snippet shows how to convert Markdown to HTML using Aspose.HTML for .NET.

 1using System.IO;
 2using Aspose.Html.Converters;
 3...
 4    // Prepare a path to a source Markdown file
 5    string sourcePath = Path.Combine(DataDir, "nature.md");
 6
 7    // Prepare a path for converted file saving 
 8    string savePath = Path.Combine(OutputDir, "nature-output.html");
 9
10    // Convert Markdown to HTML document
11    Converter.ConvertMarkdown(sourcePath, savePath);

You can view the resulting HTML file by following the link – nature-output.html

Download our Aspose.HTMLfor .NET library allows you to successfully, quickly, and easily convert your HTML, MHTML, EPUB, SVG, and Markdown documents to the most popular formats.

You can check the quality of Markdown to HTML conversion with our online MD to HTML Converter. Upload, convert your files and get results in a few seconds. Try our forceful Markdown to HTML Converter for free now!

Text “Banner MD to HTML Converter”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.