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.


Get Started with Aspose.HTML for .NET API

Install Aspose.HTML for .NET from NuGet with the .NET CLI:

dotnet add package Aspose.HTML

You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the installation guide for setup details, or request a temporary license to evaluate the API without evaluation limitations.

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.