C# API to Parse Markdown Files

Create elements, remove or edit MD document content using C# library features.

Parse Markdown Files Using C#

Aspose.HTML for .NET library provides Markdown parsing API for the C# platform. The Aspose.Html.Toolkit.Markdown.Parser namespace contains classes and methods to provide full Markdown parsing and rendering.

You can сreate, edit, save, merge, convert MD files to other file formats, and add links, lists, code blocks, images and other elements into Markdown files by following the links:



How to Parse Markdown in C#

The task of editing Markdown programmatically in C# is modifying elements in the MD document tree. Aspose.HTML for .NET API supports a set of MD elements that are defined in GitHub Flavored Markdown Spec, along with rules about how the elements can be nested. Moreover, 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. Consider simple steps to parse and edit Markdown. Let’s parse Markdown and edit text content into it:


Parse and Edit Markdown in C#

    // 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);
    
    // Create a TreeWalker object and navigate through the syntax tree starting at the first node
    using var iterator = markdown.CreateTreeWalker(markdown.FirstChild);
    while (iterator.NextNode() != null)
    {
        // Check the type of the current node as EmphasisSyntax
        if (iterator.CurrentNode is EmphasisSyntaxNode)
        {
            var node = (EmphasisSyntaxNode)iterator.CurrentNode;
            var n = node.FirstChild;
            while (n != null)
            {
                var next = n.NextSibling;
                // Remove the EmphasisSyntax node
                node.RemoveChild(n);
                n = next;
            }

            // Get a SyntaxFactory to create new elements 
            var syntax = markdown.SyntaxFactory;

            // Add the text to a new node
            node.AppendChild(syntax.Text("new text with"));

            // Add whitespase
            node.AppendChild(syntax.Whitespace());

            // Create an emphasis object
            var emphasis = syntax.Emphasis(Emphasis.Strong);

            // Add the text into empasis and accumulate a new node
            emphasis.AppendChild(syntax.Text("strong emphasis!"));
            node.AppendChild(emphasis);
            break;
        }
    }

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

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



Steps to Parse and Edit Markdown in C#

Consider simple steps to parse Markdown and edit existing text content:

  1. Specify the path to the source MD file and use the MarkdownParser() constructor to initialize a new instance of the MarkdownParser class.
  2. Call the ParseFile() method to parse Markdown and get a syntax tree.
  3. Create a TreeWalker object and navigate through the Markdown syntax tree and remove unnecessary content – EmphasisSyntax node.
  4. Use the SyntaxFactory property to get a syntax factory to create new elements.
  5. Create a new text node and add text content to the new emphasis.
  6. Use AppendChild() method to add the new node to the syntax tree.
  7. Save the edited MD file with Save() method.

Documentation

To learn more about Aspose.HTML API, please visit our documentation guide. You can download the examples from the GitHub repository. They are open source and can be freely used in your own applications.

Markdown is a simple markup language that allows you to format plain text. John Gruber, the author of MD, 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. The Markdown Syntax documentation article provides information on the main Markdown elements, details and examples of the Markdown syntax.



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.

Other Supported .NET API Features

Using Aspose.HTML, an advanced web scraping and HTML parsing library, you can create, edit, navigate through nodes, extract data, merge and convert HTML, XHTML, MD, EPUB, and MHTML files to PDF, XPS, DOCX, Images and other formats.