Tuesday, April 28, 2015

Extract href tag from string in c#

If you want to extract href tag from the url in c#, then you can use the HtmlAgulityPack. You can find more information about the HtmlAgilityPack here.

You need to add HtmlAgilityPack NuGet package using "Manage NuGet Packages.." menu which will appear after right clicking on the Project in Visual Studio. You can get more information about the HtmlAgilityPack NuGet package here.

Use the following code snippet for extracting href tag from string:

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            var node = doc.DocumentNode.SelectSingleNode("//a");
            strhref = node.Attributes["href"].Value;