Convert HTML list into a C# list
Article have related YouTube video
If you wonder if there is a good way to convert a string with a HTML list like a ul tag or a ol tag with some li tags inside to a C# list, then you came to the right place, because there is a very good way to do it by using the nuget package called HtmlAgilityPack.
Before we can do any coding, you need to go and install the newest version of HtmlAgilityPack.
When the package is installed, you can create a new instance of a HtmlDocument. We can then go an load the actual html string into our HtmlDocument. This is how my string looks like:
So as you can see the string is a ul list with some li tags inside. The thing that HtmlAgilityPack then helps us to do, is actually just to extract these li tags in the list. Getting the value and then add that value to a list with a foreach. The code will look like this:
The example is mostly created for Blazor, but with smaller adjustments you can easily use it for a web app, api or console instead.