Convert Markdown to HTML | C#

MD 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.

Code example in C# to convert document formats
Input file
Upload a file you want to convert
Output format
Select the target format

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.
using System.IO;
using Aspose.Html.Converters;
...
    // Prepare a path to a source Markdown file
    string sourcePath = Path.Combine(OutputDir, "document.md");

    // Prepare a simple Markdown example
    var code = "### Hello, World!" +
               "\r\n" +
               "[visit applications](https://products.aspose.app/html/family)";
    // Create a Markdown file
    File.WriteAllText(sourcePath, code);
    
    // Prepare a path for converted file saving 
    string savePath = Path.Combine(OutputDir, "document-output.html");
    
    // Convert Markdown to HTML document
    Converter.ConvertMarkdown(sourcePath, savePath);

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.

using System.IO;
using Aspose.Html.Converters;
...
    // Prepare a path to a source Markdown file
    string sourcePath = Path.Combine(DataDir, "nature.md");

    // Prepare a path for converted file saving 
    string savePath = Path.Combine(OutputDir, "nature-output.html");
    
    // Convert Markdown to HTML document
    Converter.ConvertMarkdown(sourcePath, savePath);

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

Text “Banner MD to HTML Converter”