Search code examples
c#asp.netparsinganglesharp

How do I get a Title of the link in AngleSharp item object?


Here is a link:

<a title = "mylink" href="mysite">content</a>

In AngleSharp object I can easily get content with this code:

string innerContent = item.TextContent;

But I need to get a title of the link and also a href. How do I do that?


Solution

  • Note that AngleSharp uses the standard DOM as defined by the W3C - thus you can just search for, e.g., "how to get href from anchor element in DOM" to retrieve an answer. For completeness, the example search query leads to (first hit on Google) Get local href value from anchor (a) tag, which answers your question.

    Just translated to C# that means

    var anchor = item as IHtmlAnchorElement; // Assumption: You have obtained it "only" as an IHtmlElement
    string title = item.Title;
    string href = item.Href;
    

    Remark: There is a difference between .GetAttribute("href") and .Href. The former is always available (even on non-IHtmlAnchorElement) and gives you the real value. The latter is a special computed version available on some elements (e.g., IHtmlAnchorElement) and will get you a normalized version, already considering the base URL of the current document.

    TL;DR: .Href will give you an absolute URL while .GetAttribute("href") may give you a relative URL.

    HTH!