Convert Markdown to HTML in C#
In daily development, Markdown is widely used in document writing, blog publishing, content management and other scenarios due to its concise syntax and easy readability. However, in many cases, it is necessary to convert Markdown to HTML for display (such as web-side rendering), and manual conversion is inefficient and error-prone. This article will share how to quickly implement efficient conversion from Markdown to HTML with the Spire.Doc for .NET library, including examples of basic usage and advanced batch conversion.
I. Environment Preparation
1. Install Spire.Doc
It is recommended to install via NuGet Package Manager, which is the most convenient way:
- In Visual Studio: Right-click the project → Manage NuGet Packages → Search for
Spire.Doc→ Install; - Package Manager Console command:
Install-Package Spire.Doc
2. Environment Compatibility
- Operating Systems: Windows, Linux, macOS (.NET Core/.NET 5+ environments);
- .NET Versions: Supports mainstream .NET frameworks without additional adaptation.
II. Basic Implementation: Markdown to HTML
Scenario 1: Convert Markdown Content from String
Suitable for dynamically generated Markdown content (such as user input, Markdown text returned by APIs). The core steps are "Create document object → Load Markdown string → Save as HTML".
Complete code example:
using Spire.Doc;
using System;
using System.IO;
namespace MarkdownToHtmlDemo
{
class Program
{
static void Main(string[] args)
{
// Define the Markdown content to be converted (title, plain text, bold, italic, hyperlink, unordered list)
string markdownContent = @"
# C# Markdown to HTML Practice
## Based on Spire.Doc for .NET
This is a piece of plain text, including **bold text**, *italic text* and [Spire.Doc Official Link](https://www.e-iceblue.com/).
### Core Advantages
- Concise and efficient Markdown syntax parsing
- Cross-.NET framework compatibility (.NET Framework/.NET Core/.NET 5+)
- Support for custom HTML output rules
- No dependency on third-party software such as Microsoft Word
### Application Scenarios
- Blog system content rendering
- Document management platform format conversion
- Content publishing system front-end display
";
// Define file paths
string markdownFilePath = "example.md"; // Path to save the Markdown file
string outputHtmlPath = "output.html"; // Path to save the converted HTML file
// Generate file from Markdown string
File.WriteAllText(markdownFilePath, markdownContent);
// Load the file
Document document = new Document();
document.LoadFromFile(markdownFilePath, FileFormat.Markdown);
// Save as HTML format
document.SaveToFile(outputHtmlPath, FileFormat.Html);
document.Close();
Console.WriteLine($"Conversion successful! HTML file path: {outputHtmlPath}");
}
}
}
Scenario 2: Convert from Local Markdown File
If the Markdown content has been saved as a local file (such as a .md file), you can directly load the file for conversion with simpler code:
using Spire.Doc;
using System;
namespace MarkdownFileToHtml
{
class Program
{
static void Main(string[] args)
{
// 1. Create a document instance
Document markdownDoc = new Document();
// 2. Load local Markdown file (specify file path and format)
string inputMdPath = "Input/Test.md";
markdownDoc.LoadFromFile(inputMdPath, FileFormat.Markdown);
// 3. Save as HTML
string outputHtmlPath = "Output/MarkdownFileToHtml.html";
markdownDoc.SaveToFile(outputHtmlPath, FileFormat.Html);
Console.WriteLine("File conversion completed!");
}
}
}
III. Advanced Usage: Batch Convert Multiple Markdown Files
To batch process all .md files in a folder, you can implement it with file traversal:
using Spire.Doc;
using System.IO;
namespace BatchMarkdownToHtml
{
class Program
{
static void Main(string[] args)
{
// Source Markdown folder
string sourceDir = "Input/MarkdownFiles/";
// Target HTML folder
string targetDir = "Output/HtmlFiles/";
if (!Directory.Exists(targetDir))
{
Directory.CreateDirectory(targetDir);
}
// Traverse all .md files
foreach (string mdFile in Directory.GetFiles(sourceDir, "*.md"))
{
// Get file name (without extension)
string fileName = Path.GetFileNameWithoutExtension(mdFile);
// Target HTML path
string htmlPath = Path.Combine(targetDir, $"{fileName}.html");
// Execute conversion
Document doc = new Document();
doc.LoadFromFile(mdFile, FileFormat.Markdown);
doc.SaveToFile(htmlPath, FileFormat.Html);
Console.WriteLine($"Converted: {mdFile} → {htmlPath}");
}
Console.WriteLine("Batch conversion completed!");
}
}
}
Spire.Doc for .NET provides C# developers with an extremely simple solution for Markdown to HTML conversion. It eliminates the need for complex syntax parsing logic, allowing basic conversion to be completed with just a few lines of code,. This solution can be widely applied in blog systems, document management platforms, content publishing systems, and other scenarios, greatly improving development efficiency.