Convert HTML to Markdown or Markdown to HTML in C#

Convert HTML Markdown C#

Markdown is a mark-up language with simple symbols that denote heading, bold, italic, underline, and much more. It is simple to learn and write so mostly online documentation and readme files are written using the markdown. You can convert markdown to HTML Webpage easily with few quick steps. Likewise, HTML to markdown conversion is also a useful feature that we will be exploring here using C# language.

HTML and Markdown Converter – API Installation

You can inter-convert the HTML webpage and Markdown format files using C# programming language. You need to install Aspose.HTML for .NET API to manipulate and convert these file formats. You can either install the API from NuGet gallery with the following installation command, or you can download the DLL file from the Downloads section, for adding it as a reference in your project.

PM> Install-Package Aspose.Html

So far you will be able to configure the API within couple of minutes. Let us proceed to walk through different possible scenarios:

Convert HTML Webpage to Markdown (.md) Programmatically in C#

You can convert HTML Webpage to Markdown in your .NET based applications. This popular conversion feature is supported in a way that you only need to call the API without getting into the details of underlying file formats. Therefore, you need to follow the below steps for HTML webpage conversion:

  1. Create a HTML file or load Existing input file
  2. Call ConvertHTML method with MarkdownSaveOptions object
  3. Save output Markdown (.md) File

The code below shows how to convert HTML Webpage to Markdown using C#:

Convert HTML Webpage to Markdown (.md) with Additional Options in C#

We have already learned the simple and basic use case of converting HTML to Markdown notation. Let us move another step further where we will be controlling the conversion process. Consider a scenario where there is a lot of irrelevant data in an HTML file but we only need to convert the contents into paragraph or hyperlink tags. We will follow the steps below for understanding and trying this feature:

  1. Load an input HTML file
  2. Initialize MarkdownSaveOptions object
  3. Set the Flag variables under Features property
  4. Save output Markdown file

The below code is based on these steps which shows how to convert HTML Webpage to Markdown with additional options using C#:

Convert Markdown (.md) to HTML Webpage Programmatically using C#

You might need to convert HTML to Markdown notation in some scenarios. For instance, some documentation section to be utilized in web application and many other use cases. You can easily convert the Markdown file to HTML with the following steps:

  1. Create a Markdown (.md) file or load input file
  2. Save output HTML file with ConvertMarkdown method

The code below shows how to convert a Markdown file (.md) to HTML Webpage using C# programming language:

Download Example Project and Sample Files

We have compiled a sample project as a demonstration of the aforementioned features where you can also download the sample files. It is a console application that you can try in order to explore and understand the markdown and HTML inter-conversion feature. So you may download the ZIP or clone the GitHub Repository for testing the features.

Conclusion

In a nutshell, we have explored the conversion of HTML Webpage and Markdown files in detail. The API can convert these files quickly and efficiently with simple API calls. Moreover, you can learn other features by visiting the Product Documentation and API References. However, in case of any concerns, please feel free to get in touch with us via the Free Support Forum.

See Also

C# Convert HTML to JPG, PNG, BMP and GIF Images with .NET