Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To check whether HTML text content is empty in C#, load the document with HTMLDocument, read document.Body.TextContent, and test it with string.IsNullOrWhiteSpace().
Aspose.HTML for .NET can parse, render, and manipulate HTML content programmatically. One common validation task is checking whether an HTML document contains DOM text or whether the page body is empty or whitespace-only.
This kind of check helps content pipelines, CMS workflows, and automated QA tools identify empty pages early. It is especially useful when processing many generated HTML files where manual review is slow and error-prone.
TextContent is a DOM-text check, not a complete visible-content or content-quality test. It can include hidden text and text inside <script> or <style> elements, while images, form values, attribute values, and CSS-generated content are not included.
TextContent Property – C# ExampleThe
TextContent property retrieves or sets the text content of an element and its descendants. It is useful when you need to check whether the <body> element contains text, independent of HTML markup.
Body property of the HTMLDocument class to retrieve the <body> element of the loaded HTML document.<body> element and check it with string.IsNullOrWhiteSpace().TextContent is null, empty, or contains only whitespace.The following C# example shows how to check if HTML text content is empty:
1// Check if HTML body is empty using C#
2
3// Prepare a path to a source HTML file
4string inputPath = Path.Combine(DataDir, "file.html");
5
6// Load the HTML document
7using (HTMLDocument document = new HTMLDocument(inputPath))
8{
9 // Get the body element
10 HTMLElement body = document.Body;
11
12 // Check if the body element contains any child nodes
13 if (!string.IsNullOrWhiteSpace(body.TextContent))
14 Console.WriteLine("Non-empty HTML elements found");
15 else
16 Console.WriteLine("No child nodes found in the body element.");
17}When HTML comes from an API, widget, CMS, or another external source, check the raw response before creating an HTMLDocument. This distinguishes a missing or whitespace-only response from valid HTML markup that contains no body text.
The following method accepts an HTML response string and returns true when its <body> contains non-whitespace DOM text:
1using System;
2using Aspose.Html;
3
4static bool HasTextContent(string html, string baseUrl)
5{
6 if (string.IsNullOrWhiteSpace(html))
7 return false;
8
9 using HTMLDocument document = new HTMLDocument(html, baseUrl);
10
11 return document.Body != null &&
12 !string.IsNullOrWhiteSpace(document.Body.TextContent);
13}Pass the source endpoint or page URL as baseUrl when relative resources in the response must be resolved. If the API returns JSON that contains an HTML property, deserialize the JSON first and pass only that property value to HasTextContent(); do not parse the complete JSON response as HTML.
This method checks for DOM text. A response containing only an image, an input control, or another non-text element returns false even though the HTML structure is not empty.
| Check | What it detects | Limitation |
|---|---|---|
Body.TextContent with string.IsNullOrWhiteSpace() | Empty or whitespace-only body DOM text. | Hidden text and <script> or <style> text can be included; images, form values, attributes, and CSS-generated content are not. |
Element-level TextContent | Empty headings, paragraphs, list items, or containers. | Descendant text is included regardless of whether CSS makes it visible. |
| DOM element count | Structurally empty pages or missing sections. | A page can have elements but still contain no body text. |
No. TextContent returns text nodes inside an element and its descendants, not markup tags or attribute values such as alt, value, and placeholder.
Yes. A page may contain images, form controls, media, or layout elements without body text. Use this check only when DOM text is the required signal, and inspect relevant elements or attributes when non-text content also counts.
Yes. TextContent can include hidden text and text inside <script> or <style> elements. It does not determine whether text is rendered, visible, or meaningful to a user.
For whole-page validation, document.Body.TextContent is a good starting point. For targeted validation, check specific elements such as headings, paragraphs, or content containers.
The current HTML specification uses the term void element for elements such as <br>, <img>, and <input> that cannot contain child content and must not have an end tag. This differs from a normal element with no content, such as <div></div>, and from an element that contains non-text content, such as <div><img src="photo.png" alt="Photo"></div>. TextContent can be empty in all three cases, so inspect the DOM structure and relevant attributes when the distinction matters. See
HTML Standard – Elements for the complete void-element definition.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.