How to Save MD 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 from scratch or open a Markdown document from a local file, edit the content of existing nodes and save or convert the edited document to the required format.
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 Save MD File in C#?

The following C# code example shows how to save an MD document. The last two steps are required to save Markdown – you can prepare a path to save the MD file and use the Save() method.

C# code to save MD

    // Specify the path to the source MD file
    var input = "C:/temp/document.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 the Markdown document

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

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



Steps to Save MD in C#

Let’s take a look at the simple steps to save MD file after editing:

  1. Specify a path to 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. Use the SyntaxFactory property to get a syntax factory to create new elements.
  4. Work with the document.
  5. Prepare the path to save MD file.
  6. Save the created MD file using Save() method.

Create from Scratch and Save MD in C#

Using the C# library, you can programmatically create, edit and save MD files. Let’s see how to save MD file created from scratch.

C# code to save MD file

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

    // Create a Markdown syntax factory
    var mdf = md.SyntaxFactory;
            
    // Create an empty paragraph
    var paragraph = mdf.Paragraph();

    // Add texts to the paragraph
    paragraph.AppendChild(mdf.Text("Markdown is a simple markup language that allows you to format plain text."));

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

    paragraph.AppendChild(mdf.Text("MD files use Markdown language that was proposed and developed by John Gruber."));

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

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

    // Save MD file
    md.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.