41516

Question:
how to get the href value only from html anchor tag using C# thank you
string ref="<a href="http://www.google.com"></a>";
//i want get result from
//string ref like
//http://www.google.com
Answer1:If you want to do this without <strong>HtmlAgilityPack</strong> then you can do this using <strong>regular Expression</strong> :
string ref= @"<a href=""http://www.google.com"">test</a>";
var regex = new Regex("<a [^>]*href=(?:'(?<href>.*?)')|(?:\"(?<href>.*?)\")", RegexOptions.IgnoreCase);
var urls = regex.Matches(ref).OfType<Match>().Select(m => m.Groups["href"].Value).SingleOrDefault();
Hope it helps you.
Answer2:You could use a HTML parsing library such as <a href="http://htmlagilitypack.codeplex.com/" rel="nofollow">Html Agility Pack</a>. For example:
using System;
using HtmlAgilityPack;
class Program
{
static void Main()
{
var doc = new HtmlDocument();
doc.LoadHtml("<a href=\"http://www.google.com\"></a>");
var nodes = doc.DocumentNode.SelectNodes("a[@href]");
foreach (var node in nodes)
{
Console.WriteLine(node.Attributes["href"].Value);
}
}
}
Answer3:Using <a href="http://htmlagilitypack.codeplex.com/" rel="nofollow">htmlagilitypack</a> .
var url= @"<a href="http://stackoverflow.com" ></a>";
HtmlDocument document = new HtmlDocument();
document.LoadHtml(url);
var tempValue= document.DocumentNode.SelectSingleNode("//a");
var link= tempValue.Attributes["href"].Value;