How to Create Markdown in C#

Markdown is a simple markup language that allows you to format plain text. Aspose.HTML for .NET library is a stand-alone solution that allows you to parse and manipulate Markdown files without using other software. You can create a Markdown document from scratch, open from a local file and manipulate by inserting new elements, removing, or editing the content of existing nodes.
The Aspose.Html.Toolkit.Markdown.Syntax namespace contains classes and methods to manipulate a Markdown syntax tree based on GitHub Flavored Markdown (GFM) Specification.
The Aspose.Html.Toolkit.Markdown.Parser namespace contains classes and methods to provide full Markdown parsing and rendering.
The Aspose.Html.Toolkit.Markdown.Extensions namespace contains classes and methods for manipulating the Markdown syntax tree based on specifications that are not part of the GFM specification.


How to Create Markdown from Scratch in C#?

Using the C# library, you can programmatically create MD files. Let’s look at how to create Markdown from scratch.

C# code to create MD file

    // Create the MarkdownSyntaxTree
    var md = new MarkdownSyntaxTree(new Configuration());

    // Use the SyntaxFactory property to get the factory for creating the Markdown syntax tree
    var mdf = md.SyntaxFactory;

    var heading2 = mdf.AtxHeading("How to create Markdown from scratch in C#?", 2);

    // Add the  headers to the Markdown syntax tree
    md.AppendChild(heading2);

    // Create and add newLineTrivia element for an empty line after heading1 
    var newLineTrivia = mdf.NewLineTrivia();
    heading2.GetTrailingTrivia().Add(newLineTrivia);

    // Create an empty paragraph
    var paragraph = mdf.Paragraph();

    // Add texts to the paragraph
    paragraph.AppendChild(mdf.Text("First, add an Aspose.HTML for .NET library reference to your C# project."));

    // Add a space after the first sentence in the paragraph through a special WhiteSpace node
    paragraph.AppendChild(mdf.Whitespace());

    paragraph.AppendChild(mdf.Text("Create the MarkdownSyntaxTree. Then use the SyntaxFactory property to get a syntax factory to create new elements."));

    // Add the filled paragraph into MD document
    md.AppendChild(paragraph);

    // Prepare a path for MD file saving 
    string savePath = Path.Combine(OutputDir, "document.md");

    // Save MD file
    md.Save(savePath);



Steps to Create Markdown in C#

Let’s take a look at the simple steps to create Markdown from scratch. The first two steps are mandatory for the creation of Markdown. The next steps are adding new elements and saving the document:

  1. Create the MarkdownSyntaxTree using MarkdownSyntaxTree() constructer.
  2. Use the SyntaxFactory property to get the factory for creating the Markdown syntax tree.
  3. Create and add new elements to a Markdown file.
  4. Save the created MD file using Save() method.

How to Open MD File in C#

The following C# code example shows how to open an existing MD document:

C# code to open MD file

    // Specify the path to the source MD file
    var input = "C:/temp/input.md";

    // Create a MarkdownParser object
    var parser = new MarkdownParser();

    // Parse the MD document and get a syntax tree
    var markdown = parser.ParseFile(input);

    // Work with Markdown document

    // Prepare a path for MD file saving 
    string savePath = Path.Combine(OutputDir, "output.md");

    // Save MD file
    markdown.Save(savePath);

Documentation

To learn more about Aspose.HTML API, please visit our documentation guide. The Markdown Syntax documentation article provides information on the main Markdown elements, details and examples of the Markdown syntax.

MD files use Markdown language that was proposed and developed by John Gruber. John Gruber designed Markdown’s formatting syntax with the goal of making it as readable as possible. 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. In addition, MD files can be converted to HTML, PDF, XPS or images to take advantage of other formats for specific tasks.


FAQ

1. How can I create, parse and edit Markdown in C#?

The Aspose.HTML Library for .NET is a stand-alone solution for working with Markdown documents that does not depend on other software. Install our C# library, add the library reference to your C# project, and programmatically work with Markdown documents.

2. Why is Markdown so popular?

Markdown is very popular among writers, developers, and content creators due to its versatility. Its advantages include easy-to-learn and use syntax, portability, flexibility, readability, and usability. Markdown is simple and intuitive and can be converted to HTML, PDF, or other formats.

3. Can I work with Markdown files on Linux, Mac OS, Android, or iOS?

You can work with Markdown documents on any operating system, whether you’re using Windows, Mac OS, Linux, Android, or iOS.

4. What file formats can be processed with the Aspose.HTML C# library?

We support several file formats that you can create, open, parse, edit, save or convert in C#. These are HTML, XHTML, MHTML and Markdown formats.



Get Started with .NET HTML API

You can use several ways to install the Aspose.HTML library for .NET on your system:

  1. Install a NuGet Package using the NuGet Package Manager GUI.
  2. Install a NuGet Package using the Package Manager Console. You may use the following command PM> Install-Package Aspose.Html.
  3. Install Aspose.HTML for .NET through MSI.

This library supports parsing of HTML5, CSS3, SVG, and HTML Canvas to construct a Document Object Model (DOM) based on the WHATWG DOM Standard. Aspose.HTML for .NET is written completely in C# and can be used to build any type of 32-bit or 64-bit .NET application including ASP.NET, WCF, WinForms & .NET Core. Before running the .NET conversion example code, make sure that you have OS like Microsoft Windows or a compatible with .NET Framework or .NET Standard, and the development environment like Microsoft Visual Studio. For more details about C# library installation and system requirements, please refer to Aspose.HTML Documentation.


System Requirements

Our APIs are supported on all major platforms and Operating Systems. Before executing the code, please ensure that you have the following prerequisites on your system.

  • Microsoft Windows or a compatible OS with .NET Framework, .NET Core, Windows Azure, Mono or Xamarin Platforms.
  • Development environment like Microsoft Visual Studio.
  • Aspose.Html for .NET DLL referenced in your project - Install from NuGet using the Download button above.