How to Create Markdown Lists in C#
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. Markdown supports ordered (numbered) and unordered (bulleted) lists. In this article, you learn about adding lists in Markdown using C# library.
Use the
Aspose.Html.Toolkit.Markdown.Syntax
and
Aspose.Html.Toolkit.Markdown.Parser
namespaces to provide full Markdown parsing and MD syntax tree management based on the GitHub Flavored Markdown (GFM) specification.
Create Markdown Ordered List in C#
Using the C# library, you can programmatically edit MD files – change the document structure and content. Let’s look at creating a new Markdown document from scratch and adding Markdown ordered list to it.
C# code to create Markdown ordered list
// Create the MarkdownSyntaxTree
var md = new MarkdownSyntaxTree(new Configuration());
// Create a Markdown syntax factory
var mdf = md.SyntaxFactory;
// Create a new empty odered list node
var orderedList = mdf.OrderedList();
// Create and add to the odered list a li1 item with the marker “1”
var li1 = mdf.OrderedListItem(1);
orderedList.AppendChild(li1);
// Create a paragraph with text content and add it to the li1 item
var paragraph = mdf.Paragraph();
var heading = mdf.AtxHeading("The first element in the Markdown ordered list is the level 3 heading.", 3);
paragraph.AppendChild(heading);
li1.AppendChild(paragraph);
//Create and add to the odered list a li2 item with the marker “2”
var li2 = mdf.OrderedListItem(2);
orderedList.AppendChild(li2);
// Create a strong emphasis with text content and add it to the li2 item
var bold = mdf.Emphasis(Emphasis.Strong);
bold.AppendChild(mdf.Text("The second item in the Markdown ordered list is in bold."));
li2.AppendChild(bold);
// Add orderedList to MD syntax tree
md.AppendChild(orderedList);
// Prepare a path for MD file saving
string savePath = Path.Combine(OutputDir, "output-odered-list.md");
// Save MD file
md.Save(savePath);
Steps to Create Markdown Ordered List in C#
Here are a few simple steps to create Markdown ordered list:
- Create the MarkdownSyntaxTree using MarkdownSyntaxTree() constructer.
- Use the SyntaxFactory property to get the factory for creating the Markdown syntax tree.
- Create a new orderedList node using the OderedList() constructor in SyntaxFactory.
- Use the OrderedListItem() constructor to create new instances of the ListItemSyntaxNode class with an ordered list item marker.
- Call the AppendChild() method of the MarkdownSyntaxNode class to add odered list items into the orderedList node.
- After all items with their content are gathered into a single orderedList element, add it to the document syntax tree.
Create Unordered Markdown List in C#
The following C# code example shows how to add unordered list to MD file:
C# code to add unordered list to MD file
// Create the MarkdownSyntaxTree
var md = new MarkdownSyntaxTree(new Configuration());
// Create a Markdown syntax factoty
var mdf = md.SyntaxFactory;
// Create a new empty unodered list node
var unorderedList = mdf.UnorderedList();
// Create and add to the unodered list a li1 element with the marker “-”
var li1 = mdf.UnorderedListItem("-");
unorderedList.AppendChild(li1);
// Create a paragraph with text content and add it to the li1 item:
var paragraph = mdf.Paragraph();
paragraph.AppendChild(mdf.Text("The first item of the unordered Markdown list."));
li1.AppendChild(paragraph);
// Create and add to the unodered list a li2 element with the marker “-”
var li2 = mdf.UnorderedListItem("-");
unorderedList.AppendChild(li2);
// Create a paragraph with text content and add it to the li2 item:
var paragraph2 = mdf.Paragraph();
paragraph2.AppendChild(mdf.Text("The second item of the unordered Markdown list."));
li2.AppendChild(paragraph2);
// Add unorderedList to MD syntax tree
md.AppendChild(unorderedList);
// Prepare a path for MD file saving
string savePath = Path.Combine(OutputDir, "markdown-unodered-list.md");
// Save MD file
md.Save(savePath);
Documentation
To learn more about Aspose.HTML API, please visit our
documentation
guide. Markdown is a simple markup language that allows you to format plain text. The
Markdown Syntax
documentation article provides information on the main Markdown elements, details and examples of the Markdown syntax.
FAQ
The Aspose.HTML for .NET API 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.
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.
You can work with Markdown documents on any operating system, whether you’re using Windows, Mac OS, Linux, Android, or iOS.
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 Aspose.HTML for .NET API
You can use several ways to install the Aspose.HTML for .NET library on your system:
- Install a NuGet Package using the NuGet Package Manager GUI.
- Install a NuGet Package using the Package Manager Console. You may use the following command
PM> Install-Package Aspose.Html
. - 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.