How do I add a reference to HTMLAgilityPack in Visual Studio Code

asp.net-core c# html-agility-pack macos

Question

I'm using Visual Studio Code and .NET core on OSX.

HtmlAgilityPack.NetCore.1.5.0.1 has been installed to the project folder. All of my project files, HTMLAgilityPack.NetCore.1.5.0.1 and all of the dependencies are visible in the explorer pain. Yet I cannot create a reference to any HtmlAgilityPack assemblies.

The code is simple. It compiles and runs.

namespace ConsoleApplication
{
    using System;
    using System.Text.RegularExpressions;
    using System.Linq;

    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello, world");
        }
    }
}

Is there a step beyond installing the nuget package that I need to perform to get this to work?

Accepted Answer

Unless I'm misunderstanding your question, this should be very straight forward. All you need to do is:

1: Add the nuget package into your project.json file like so, then run dotnet restore in the same directory as your project.json file to restore your newly added package.

...
},
"dependencies": {
    "HtmlAgilityPack.NetCore": "1.5.0.1"
},
"frameworks": {
...

2: Add the following using statement to the top of your code.

using HtmlAgilityPack;

This works for me:

namespace ConsoleApplication
{
    using System;
    using HtmlAgilityPack;

    public class Program
    {
        public static void Main(string[] args)
        {
            HtmlDocument doc = new HtmlDocument();

            Console.WriteLine("Hello, world");
        }
    }
}

Note: As you're using Visual Studio Code on OSX, you can reference a class and then press the shortcut CMD + . to bring up Visual Studio Code's tool window and automatically import your missing using statement.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why