Element.GetElementsByTagName

Element.GetElementsByTagName method

Returns HTMLCollection object containing all elements with a given tag name, in document order.

public HTMLCollection GetElementsByTagName(string name)
ParameterTypeDescription
nameStringThe tag name. String representation of tag name.

Return Value

An HTMLCollection object is an array-like list of elements.

Remarks

Refer to official spec.

You may also be interested in documentation.

You can download the complete examples and data files from GitHub.

Examples

# Html input content
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
</head>
<body>
<div id="divElementContainerId">
	<p class="pStyle">The paragraph styled pStyle class content...</p>
	<p>The second paragraph content...</p>
	<p>The third paragraph content...</p>
	<div class="pStyle">The div element styled pStyle class...</div>
</div>
</body>
</html>

# C# code
using System;
using Aspose.Html;
using Aspose.Html.Collections;
using Aspose.Html.Dom;
...
using (var document = new HTMLDocument(inputHtmlPath))
{
	// User code goes here

	HTMLCollection htmlCollection = document.GetElementsByTagName("p");
	Console.WriteLine($"Found: {htmlCollection.Length}" );
	foreach (Element element in htmlCollection)
	{
		Console.WriteLine(element.InnerHTML);
	}
         
	// User code goes here
}

*inputHtmlPath - user input html file.

Console output

Found: 3

The paragraph styled pStyle class content…

The second paragraph content…

The third paragraph content…

See Also