41516

get href value from html anchor tag C#

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;

Recommend

  • LINQ's *OrDefault throws exceptions in MVC3 + ActiveRecord
  • Regular Expression Negative Lookbehind Alternative for VBScript
  • LINQ's *OrDefault throws exceptions in MVC3 + ActiveRecord
  • Regex - Replacing multiple spaces with tab
  • IIS 7 Rewrite web.config serverVariables directive not working in sub folder
  • Split string to array from text and html tag
  • c# Extract String By Regex
  • Getting Results from Search entity for twitter search
  • Excel VBA: search a string to find the first non-text character
  • RegEx help to remove noise words or stop words from string
  • Github ERROR: Repository not found (yes, another one)
  • Django how to modify database records by template
  • Can I put a + sign in a folder with IIS?
  • Dynamically generated lookup key for IQueryable
  • Why isn't \\\\ interpreted as a blackslash in this regex?
  • Open hyperlink on click on an ggplot/plotly chart
  • Symfony2 redirect to https route fails (uses wrong port)
  • C++ and JS RegEx equivalent of \\p{L}
  • Scala using regex with or syntax in match case statement
  • Negating Regex PO BOX
  • in batch how do i use taskkill properly
  • Efficient User-Agent Regex to find Safari in Python
  • Google Places API - Find a company's CID and LRD
  • Clear activity stack before launching another activity
  • Hide HTML elements without javascript, only CSS
  • How to have background script and something similar to a default popup?
  • Refering to the class itself from within a class mehod in Objective C
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • Regex thinks I'm nesting, but I'm not
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • How to delete a row from a dynamic generate table using jquery?
  • using HTMLImports.whenReady not working in chrome
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Django query for large number of relationships