Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract href tag from a string in C#?

Tags:

string

c#

href

tags

I have a method that returns a string in the following format:

string tableTag = "<th><a href="Boot_53.html">135 Boot</a></th>"

I want to get the value of the href attribute and store it into another string called link:

string link = "Boot_53.html"

In other words, link should be assigned the href attribute in the string. How can I accomplish that?

like image 892
Mamoon Braiga Avatar asked Mar 03 '14 15:03

Mamoon Braiga


2 Answers

You could use an HTML parser such as HTML agility pack to parse the input HTML and extract the information you are looking for:

using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        var doc = new HtmlDocument();
        string tableTag = "<th><a href=\"Boot_53.html\">135 Boot</a></th>";
        doc.LoadHtml(tableTag);

        var anchor = doc.DocumentNode.SelectSingleNode("//a");
        if (anchor != null)
        {
            string link = anchor.Attributes["href"].Value;
            Console.WriteLine(link);
        }
    }
}
like image 187
Darin Dimitrov Avatar answered Sep 24 '22 00:09

Darin Dimitrov


If you know that the html is actually a xhtml (an html which conforms to the xml standarts [more or less]) you can parse is simply with tools dedicated to xml (which are generally simpler than those for html).

var hrefLink = XElement.Parse("<th><a href=\"Boot_53.html\">135 Boot</a></th>")
                       .Descendants("a")
                       .Select(x => x.Attribute("href").Value)
                       .FirstOrDefault();
like image 30
Shlomi Borovitz Avatar answered Sep 27 '22 00:09

Shlomi Borovitz