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.


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.