How to get HTML tag attributes using C-Sharp


If HTML text is XHTML then you can parse it as normal XML and get what you want. But for normal HTML if you want to get values of particular tag attributes then you can use regular expressions to do this in C#.

Below is example for finding a tag attribute values.

Example

Use below method to get attribute values of a tag

/// <summary>
/// Method to get all attributes values of a tag in HTMl string
/// </summary>
/// <param name="htmlSource">HTML text</param>
/// <param name="tag">tag name like IMG</param>
/// <param name="attr">attribute name like href for IMG</param>
/// <returns></returns>
public List<string> GetAttrsFromSource(string htmlSource, string tag, string attr)
{
    List<string> links = new List<string>();
    string regexImgSrc = @"<" + tag + @"[^>]*?" + attr + @"s*=s*[""\']?([^\'"" >]+?)[ \'""][^>]*?>";
    MatchCollection matchesImgSrc = Regex.Matches(htmlSource, regexImgSrc, RegexOptions.IgnoreCase | RegexOptions.Singleline);
    foreach (Match m in matchesImgSrc)
    {
        string href = m.Groups[1].Value;
        links.Add(href);
    }
    return links;
}