HtmlTokenizerReadNextToken Method

Read the next token.

Definition

Namespace: MimeKit.Text
Assembly: MimeKit.Core (in MimeKit.Core.dll) Version: 5.0.0
C#
public bool ReadNextToken(
	out HtmlToken?? token
)

Parameters

token  HtmlToken
The token that was read.

Return Value

Boolean
if the next token was read; otherwise, .

Remarks

Reads the next token.

Example

C#
                    public static IList<string> ExtractLinks (string html)
{
    var links = new List<string> ();

    using (var reader = new StringReader (html)) {
        // HtmlTokenizer is a forward-only HTML5 tokenizer. It does not build a DOM which
        // makes it fast and well-suited for scanning or rewriting HTML.
        var tokenizer = new HtmlTokenizer (reader);

        while (tokenizer.ReadNextToken (out var token)) {
            if (token.Kind != HtmlTokenKind.Tag)
                continue;

            var tag = (HtmlTagToken) token;

            // Tag and attribute names are mapped to HtmlTagId and HtmlAttributeId values
            // for fast comparisons.
            if (tag.Id != HtmlTagId.A || tag.IsEndTag)
                continue;

            foreach (var attribute in tag.Attributes) {
                if (attribute.Id == HtmlAttributeId.Href && !string.IsNullOrEmpty (attribute.Value))
                    links.Add (attribute.Value!);
            }
        }
    }

    return links;
}
                  

See Also