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 for .NET 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:
- Prepare a source Markdown document. In the example, we create a Markdown file from code.
- Prepare a path for converted file saving.
- Use the
ConvertMarkdown() method of the Converter class to save Markdown as an HTML file. You need to pass the
sourcePath
andsavePath
to the ConvertMarkdown() method for Markdown to HTML conversion.
1// Prepare a path to a source Markdown file
2string sourcePath = Path.Combine(OutputDir, "document.md");
3
4// Prepare a simple Markdown example
5var code = "### Hello, World!" +
6 "\r\n" +
7 "[visit applications](https://products.aspose.app/html/family)";
8// Create a Markdown file
9File.WriteAllText(sourcePath, code);
10
11// Prepare a path to save the converted file
12string savePath = Path.Combine(OutputDir, "document-output.html");
13
14// Convert Markdown to HTML document
15Converter.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:
- Open an existing Markdown document. In the example, we load a Markdown file from a local file system ( nature.md).
- Prepare a path for converted file saving.
- Use the
ConvertMarkdown() method of the Converter class to save Markdown as an HTML file. You need to pass the
sourcePath
andsavePath
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.
1// Prepare a path to a source Markdown file
2string sourcePath = Path.Combine(DataDir, "nature.md");
3
4// Prepare a path to save the converted file
5string savePath = Path.Combine(OutputDir, "nature-output.html");
6
7// Convert Markdown to HTML
8Converter.ConvertMarkdown(sourcePath, savePath);
You can view the resulting HTML file by following the link – nature-output.html
Download our Aspose.HTML for .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!