diff --git a/html-navigation/add-aria-label-attributes-to-navigation-links-without-descriptive-text-screen-readers.cs b/html-navigation/add-aria-label-attributes-to-navigation-links-without-descriptive-text-screen-readers.cs new file mode 100644 index 0000000..7179825 --- /dev/null +++ b/html-navigation/add-aria-label-attributes-to-navigation-links-without-descriptive-text-screen-readers.cs @@ -0,0 +1,75 @@ +// Add aria‑label attributes to navigation links lacking descriptive text for screen readers. + +using System; +using System.IO; + +class Program +{ + static void Main(string[] args) + { + try + { + string html = "
" +
- "
" +
- "";
-
- // Define input and output file paths
- string inputPath = "sample.html";
- string outputPath = "output.html";
-
- // Write the sample HTML to a file
- File.WriteAllText(inputPath, htmlContent, Encoding.UTF8);
-
- // Load the HTML document
- Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(inputPath);
-
- // Get all 
Text
";
+ Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank");
Aspose.Html.Collections.HTMLCollection images = document.GetElementsByTagName("img");
-
- // Iterate over each image element
foreach (Aspose.Html.Dom.Element node in images)
{
Aspose.Html.HTMLImageElement img = node as Aspose.Html.HTMLImageElement;
@@ -43,9 +24,7 @@ static void Main()
}
}
}
-
- // Save the modified document
- document.Save(outputPath);
+ document.Save("output.html");
}
catch (Exception ex)
{
diff --git a/html-navigation/add-missing-charset-meta-tag-detected-file-encoding-improve-compatibility.cs b/html-navigation/add-missing-charset-meta-tag-detected-file-encoding-improve-compatibility.cs
new file mode 100644
index 0000000..973dd92
--- /dev/null
+++ b/html-navigation/add-missing-charset-meta-tag-detected-file-encoding-improve-compatibility.cs
@@ -0,0 +1,58 @@
+// Add a missing charset meta tag based on detected file encoding to improve compatibility.
+
+using System;
+using System.IO;
+using System.Text;
+
+class Program
+{
+ static void Main()
+ {
+ try
+ {
+ // Define input and output paths
+ string sourcePath = "input.html";
+ string outputPath = "output.html";
+
+ // Create a minimal sample HTML file if it does not exist
+ if (!File.Exists(sourcePath))
+ {
+ string sampleHtml = "Hello World
"; + File.WriteAllText(sourcePath, sampleHtml, Encoding.UTF8); + } + + // Detect encoding (for this example we assume UTF-8) + string encodingName = "utf-8"; + string htmlContent = File.ReadAllText(sourcePath, Encoding.GetEncoding(encodingName)); + + // Add charset meta tag if missing + if (!htmlContent.Contains("", StringComparison.OrdinalIgnoreCase)) + { + htmlContent = htmlContent.Replace("", "", StringComparison.OrdinalIgnoreCase); + } + else if (htmlContent.Contains("", StringComparison.OrdinalIgnoreCase)) + { + htmlContent = htmlContent.Replace("", "", StringComparison.OrdinalIgnoreCase); + } + else + { + htmlContent = "" + htmlContent; + } + } + + // Load HTML content into Aspose.HTML document + Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); + + // Save the modified document + document.Save(outputPath); + + Console.WriteLine($"HTML saved to: {outputPath}"); + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/html-navigation/add_missing_doctype_declaration_at_beginning_of_html_file_to_ensure_standards_mode.cs b/html-navigation/add-missing-doctype-declaration-beginning-html-file-ensure-standards-mode.cs similarity index 57% rename from html-navigation/add_missing_doctype_declaration_at_beginning_of_html_file_to_ensure_standards_mode.cs rename to html-navigation/add-missing-doctype-declaration-beginning-html-file-ensure-standards-mode.cs index 32f60c1..b782791 100644 --- a/html-navigation/add_missing_doctype_declaration_at_beginning_of_html_file_to_ensure_standards_mode.cs +++ b/html-navigation/add-missing-doctype-declaration-beginning-html-file-ensure-standards-mode.cs @@ -2,7 +2,7 @@ using System; using System.IO; -using Aspose.Html; +using System.Text; class Program { @@ -13,18 +13,15 @@ static void Main() string inputPath = "input.html"; string outputPath = "output.html"; - // Create a minimal sample HTML file without DOCTYPE if it doesn't exist + // Create a minimal sample HTML file if it does not exist if (!File.Exists(inputPath)) { string sampleHtml = "Hello, World!
"; - File.WriteAllText(inputPath, sampleHtml); + File.WriteAllText(inputPath, sampleHtml, Encoding.UTF8); } - // Load the HTML document using Aspose.Html - Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(inputPath); - - // Get the outer HTML of the document - string html = document.DocumentElement.OuterHTML; + // Read the HTML content + string html = File.ReadAllText(inputPath, Encoding.UTF8); // Add DOCTYPE if missing if (!html.TrimStart().StartsWith("\n" + html; } - // Save the modified HTML to the output file - File.WriteAllText(outputPath, html); + // Load the HTML into Aspose.HTML document + Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(html, "about:blank"); - Console.WriteLine("HTML file processed successfully. Output saved to: " + outputPath); + // Save the updated document + document.Save(outputPath); } catch (Exception ex) { - Console.WriteLine("Error: " + ex.Message); + Console.WriteLine(ex.Message); } } } \ No newline at end of file diff --git a/html-navigation/add-nofollow-attribute-external-links-search-engine-crawling.cs b/html-navigation/add-nofollow-attribute-external-links-search-engine-crawling.cs new file mode 100644 index 0000000..bde2a55 --- /dev/null +++ b/html-navigation/add-nofollow-attribute-external-links-search-engine-crawling.cs @@ -0,0 +1,36 @@ +// Add a nofollow attribute to external links to control search engine crawling. + +using System; + +class Program +{ + static void Main() + { + try + { + string htmlContent = "Hello World
"; - File.WriteAllText(sourcePath, sampleHtml, Encoding.UTF8); - } - - // Detect encoding (example: Windows-1252) - string encodingName = "windows-1252"; - Encoding encoding = Encoding.GetEncoding(encodingName); - - // Read HTML content using the detected encoding - string htmlContent = File.ReadAllText(sourcePath, encoding); - - // Load HTML into Aspose.HTML document - Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, ""); - - // Create element - Aspose.Html.HTMLElement meta = (Aspose.Html.HTMLElement)document.CreateElement("meta"); - meta.SetAttribute("charset", encoding.WebName); - - // Append meta element to - Aspose.Html.HTMLElement head = (Aspose.Html.HTMLElement)document.GetElementsByTagName("head")[0]; - head.AppendChild(meta); - - // Save the modified document - document.Save(outputPath); - - Console.WriteLine($"Document saved to: {outputPath}"); - } - catch (Exception ex) - { - Console.WriteLine($"Error: {ex.Message}"); - } - } -} \ No newline at end of file diff --git a/html-navigation/add_nofollow_attribute_external_links_control_search_engine_crawling.cs b/html-navigation/add_nofollow_attribute_external_links_control_search_engine_crawling.cs deleted file mode 100644 index 50f1c07..0000000 --- a/html-navigation/add_nofollow_attribute_external_links_control_search_engine_crawling.cs +++ /dev/null @@ -1,43 +0,0 @@ -// Add a nofollow attribute to external links to control search engine crawling. - -namespace Example -{ - class Program - { - static void Main(string[] args) - { - try - { - string inputPath = "sample.html"; - string outputPath = "output.html"; - - if (!System.IO.File.Exists(inputPath)) - { - string sampleHtml = "Hello World
"; + var document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); + + var head = document.QuerySelector("head") as Aspose.Html.HTMLElement; + if (head == null) + { + head = (Aspose.Html.HTMLElement)document.CreateElement("head"); + var htmlElement = document.DocumentElement; + var body = document.Body; + htmlElement.InsertBefore(head, body); + } + + var link = (Aspose.Html.HTMLElement)document.CreateElement("link"); + link.SetAttribute("rel", "stylesheet"); + link.SetAttribute("href", "styles.css"); + head.AppendChild(link); + + string outputPath = "output.html"; + document.Save(outputPath); + Console.WriteLine("Document saved to " + outputPath); + } + catch (Exception ex) + { + Console.WriteLine("Error: " + ex.Message); + } + } +} \ No newline at end of file diff --git a/html-navigation/append_new_stylesheet_link_element_to_head_reference_external_css_file.cs b/html-navigation/append_new_stylesheet_link_element_to_head_reference_external_css_file.cs deleted file mode 100644 index c3ce6ff..0000000 --- a/html-navigation/append_new_stylesheet_link_element_to_head_reference_external_css_file.cs +++ /dev/null @@ -1,50 +0,0 @@ -// Append a new stylesheet link element to the head to reference an external CSS file. - -using System; -using System.IO; -using System.Linq; -using Aspose.Html; - -class Program -{ - static void Main() - { - try - { - string inputPath = "sample.html"; - string outputPath = "output.html"; - - // Create a minimal HTML file - string htmlContent = ""; - File.WriteAllText(inputPath, htmlContent); - - // Load the document - HTMLDocument document = new HTMLDocument(inputPath); - - // Get or create the element - HTMLElement head = Enumerable.First(document.GetElementsByTagName("head")) as HTMLElement; - if (head == null) - { - head = (HTMLElement)document.CreateElement("head"); - document.DocumentElement.InsertBefore(head, document.Body); - } - - // Create the element for external CSS - HTMLElement link = (HTMLElement)document.CreateElement("link"); - link.SetAttribute("rel", "stylesheet"); - link.SetAttribute("href", "styles.css"); - - // Append the link to the head - head.AppendChild(link); - - // Save the modified document - document.Save(outputPath); - - Console.WriteLine("External stylesheet link added. Document saved to: " + outputPath); - } - catch (Exception ex) - { - Console.WriteLine("Error: " + ex.Message); - } - } -} \ No newline at end of file diff --git a/html-navigation/batch_process_html_folder_extract_headings_hierarchy_save_json_outline.cs b/html-navigation/batch-process-folder-html-files-extracting-headings-hierarchy-saving-each-json-outline.cs similarity index 66% rename from html-navigation/batch_process_html_folder_extract_headings_hierarchy_save_json_outline.cs rename to html-navigation/batch-process-folder-html-files-extracting-headings-hierarchy-saving-each-json-outline.cs index 5350ef9..d7bc0e5 100644 --- a/html-navigation/batch_process_html_folder_extract_headings_hierarchy_save_json_outline.cs +++ b/html-navigation/batch-process-folder-html-files-extracting-headings-hierarchy-saving-each-json-outline.cs @@ -1,9 +1,10 @@ // Batch process a folder of HTML files, extracting headings hierarchy and saving each as a JSON outline. using System; -using System.IO; using System.Collections.Generic; +using System.IO; using System.Text.Json; +using Aspose.Html; class OutlineNode { @@ -18,8 +19,8 @@ static void Main() { try { - string inputFolder = "InputHtml"; - string outputFolder = "OutputJson"; + string inputFolder = "HTMLFiles"; + string outputFolder = "JsonOut"; if (!Directory.Exists(inputFolder)) Directory.CreateDirectory(inputFolder); @@ -27,29 +28,38 @@ static void Main() Directory.CreateDirectory(outputFolder); // Create a sample HTML file if none exist - if (Directory.GetFiles(inputFolder, "*.html").Length == 0) + string[] existingFiles = Directory.GetFiles(inputFolder, "*.html"); + if (existingFiles.Length == 0) { string samplePath = Path.Combine(inputFolder, "sample.html"); - File.WriteAllText(samplePath, "Paragraph A
+Paragraph A
+Paragraph B
+This is italic and another example.
"; + + // Load HTML document from inline content + Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); + + // Find all elements + Aspose.Html.Collections.NodeList iElements = document.QuerySelectorAll("i"); + + // Replace each with + for (int index = 0; index < iElements.Length; index++) + { + Aspose.Html.Dom.Node node = iElements[index]; + Aspose.Html.HTMLElement iElement = node as Aspose.Html.HTMLElement; + if (iElement != null) + { + Aspose.Html.HTMLElement emElement = (Aspose.Html.HTMLElement)document.CreateElement("em"); + emElement.InnerHTML = iElement.InnerHTML; + iElement.ParentNode.ReplaceChild(emElement, iElement); + } + } + + // Save the modified document + string outputPath = "output.html"; + document.Save(outputPath); + System.Console.WriteLine($"Modified HTML saved to: {outputPath}"); + } + catch (Exception ex) + { + System.Console.WriteLine($"Error: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/html-navigation/convert-all-relative-image-source-urls-to-absolute-urls-based-on-document-base-tag.cs b/html-navigation/convert-all-relative-image-source-urls-to-absolute-urls-based-on-document-base-tag.cs new file mode 100644 index 0000000..7e26964 --- /dev/null +++ b/html-navigation/convert-all-relative-image-source-urls-to-absolute-urls-based-on-document-base-tag.cs @@ -0,0 +1,60 @@ +// Convert all relative image source URLs to absolute URLs based on the document’s base tag. + +using System; + +class Program +{ + static void Main() + { + try + { + // Sample HTML with a base tag and relative image sources + string htmlContent = @" + + +
+
+ World
"; - string baseUri = ""; + string htmlContent = "First line.
Second line.
Hello John,
+We are excited to share the latest updates with you.
+Visit our website for more details.
+Best regards,
The Team
| Name | Age |
|---|---|
| Alice | 30 |
| Bob | 25 |
This is a sample email.
Visit our site for more info.
| Name | Age | City |
|---|---|---|
| Alice | 30 | New York |
| Bob | 25 | London |
Text
"); + } + + Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(inputPath); + + RemoveEmptyElements(document.DocumentElement); + + document.Save(outputPath); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + + static void RemoveEmptyElements(Aspose.Html.Dom.Node node) + { + var child = node.FirstChild; + while (child != null) + { + var next = child.NextSibling; + + // Recursively process child nodes first + RemoveEmptyElements(child); + + // Check if the child is an element and is empty + if (child is Aspose.Html.Dom.Element element) + { + bool hasNoChildren = element.FirstChild == null; + bool hasNoText = string.IsNullOrWhiteSpace(element.TextContent); + if (hasNoChildren && hasNoText) + { + node.RemoveChild(child); + } + } + + child = next; + } + } +} \ No newline at end of file diff --git a/html-navigation/detect-broken-links-by-sending-http-head-requests-to-each-extracted-url-and-logging-failures.cs b/html-navigation/detect-broken-links-by-sending-http-head-requests-to-each-extracted-url-and-logging-failures.cs new file mode 100644 index 0000000..c6f4928 --- /dev/null +++ b/html-navigation/detect-broken-links-by-sending-http-head-requests-to-each-extracted-url-and-logging-failures.cs @@ -0,0 +1,69 @@ +// Detect broken links by sending HTTP HEAD requests to each extracted URL and logging failures. + +using System; +using System.Collections.Generic; + +class Program +{ + static void Main() + { + try + { + // Sample HTML content with links + string htmlContent = "" + + "Valid Link" + + "Broken Link" + + ""; + + // Load HTML document from string (using a dummy base URI) + Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); + + ListSample paragraph"; + + // Load HTML content using the two-argument constructor (inline content) + HTMLDocument document = new HTMLDocument(htmlContent, "about:blank"); + + // Retrieve the corrected HTML after parsing + Element rootElement = document.DocumentElement; + string correctedHtml = rootElement.OuterHTML; + + Console.WriteLine("Original HTML:"); + Console.WriteLine(htmlContent); + Console.WriteLine(); + Console.WriteLine("Corrected HTML after parsing:"); + Console.WriteLine(correctedHtml); + } + catch (Exception ex) + { + Console.WriteLine("Error: " + ex.Message); + } + } +} \ No newline at end of file diff --git a/html-navigation/detect-duplicate-meta-tags-retain-first-occurrence-avoid-conflicts.cs b/html-navigation/detect-duplicate-meta-tags-retain-first-occurrence-avoid-conflicts.cs new file mode 100644 index 0000000..f1062e1 --- /dev/null +++ b/html-navigation/detect-duplicate-meta-tags-retain-first-occurrence-avoid-conflicts.cs @@ -0,0 +1,66 @@ +// Detect duplicate meta tags and retain only the first occurrence to avoid conflicts. + +using System; +using System.Collections.Generic; + +class Program +{ + static void Main() + { + try + { + string htmlContent = "
" + + "" + + "" + + "" + + "" + + "" + + "Hello World
"; + + var document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); + + var metaElements = document.GetElementsByTagName("meta"); + + var metaList = new ListHello
\n"; File.WriteAllText(inputPath, sampleHtml); } // Load the HTML document - using (HTMLDocument document = new HTMLDocument(inputPath)) - { - // Collect TODO comments - ListHello World
"; + Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); - // Load the HTML document - Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, string.Empty); - - // Get the element(s) - Aspose.Html.Collections.HTMLCollection htmlElements = document.GetElementsByTagName("html"); - - for (int i = 0; i < htmlElements.Length; i++) + Aspose.Html.Collections.HTMLCollection elements = document.GetElementsByTagName("html"); + for (int i = 0; i < elements.Length; i++) { - Aspose.Html.Dom.Element element = (Aspose.Html.Dom.Element)htmlElements[i]; + Aspose.Html.Dom.Element element = (Aspose.Html.Dom.Element)elements[i]; string lang = element.GetAttribute("lang"); if (string.IsNullOrWhiteSpace(lang)) { @@ -27,11 +25,9 @@ static void Main() } } - // Save the modified document string outputPath = "output.html"; document.Save(outputPath); - - Console.WriteLine("Document saved to " + outputPath); + Console.WriteLine("Processed HTML saved to: " + outputPath); } catch (Exception ex) { diff --git a/html-navigation/detect-list-elements-with-tabindex-attributes-evaluate-keyboard-navigation-order.cs b/html-navigation/detect-list-elements-with-tabindex-attributes-evaluate-keyboard-navigation-order.cs new file mode 100644 index 0000000..ced8f14 --- /dev/null +++ b/html-navigation/detect-list-elements-with-tabindex-attributes-evaluate-keyboard-navigation-order.cs @@ -0,0 +1,28 @@ +// Detect and list all elements with tabindex attributes to evaluate keyboard navigation order. + +using System; + +class Program +{ + static void Main() + { + try + { + string html = "LinkHello
"; + Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); + + Aspose.Html.Collections.HTMLCollection styleElements = document.GetElementsByTagName("style"); + + for (int i = styleElements.Length - 1; i >= 0; i--) + { + Aspose.Html.Dom.Element style = (Aspose.Html.Dom.Element)styleElements[i]; + if (string.IsNullOrWhiteSpace(style.TextContent)) + { + if (style.ParentNode != null) + { + style.ParentNode.RemoveChild(style); + } + } + } + + string outputPath = "output.html"; + document.Save(outputPath); + System.Console.WriteLine("EmptyHello World
"; - - // Load the HTML document - Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent); - - // Get all + -Paragraph 1
- Span -World
+ Sample "; - // Load the HTML document from the string - var document = new Aspose.Html.HTMLDocument(htmlContent); + // Load the HTML document from the string (inline content) + HTMLDocument document = new HTMLDocument(htmlContent, "about:blank"); // Select all elements that have a class attribute - Aspose.Html.Collections.NodeList elements = document.QuerySelectorAll("[class]"); + NodeList elements = document.QuerySelectorAll("[class]"); // Dictionary to hold class name frequencies - var classFrequency = new Dictionary| R1C1 | R1C2 |
| R2C1 | R2C2 |
| A |
| 10 | abc |
| 20 | 30 |
Hello World
"; + byte[] bytes = System.Text.Encoding.UTF8.GetBytes(htmlContent); + using (var inputStream = new System.IO.MemoryStream(bytes)) + using (var document = new Aspose.Html.HTMLDocument(inputStream, "about:blank")) + { + var metaElements = document.GetElementsByTagName("meta"); + string author = null; + foreach (Aspose.Html.Dom.Element meta in metaElements) + { + if (meta.GetAttribute("name") == "author") + { + author = meta.GetAttribute("content"); + break; + } + } + System.Console.WriteLine("Author: " + (author ?? "Not found")); + } + } + catch (System.Exception ex) + { + System.Console.WriteLine(ex.Message); + } + } + } +} \ No newline at end of file diff --git a/html-navigation/extract-breadcrumb-navigation-links-locating-ordered-list-elements-specific-class-names.cs b/html-navigation/extract-breadcrumb-navigation-links-locating-ordered-list-elements-specific-class-names.cs new file mode 100644 index 0000000..95eec82 --- /dev/null +++ b/html-navigation/extract-breadcrumb-navigation-links-locating-ordered-list-elements-specific-class-names.cs @@ -0,0 +1,29 @@ +// Extract breadcrumb navigation links by locating ordered list elements with specific class names. + +namespace Example +{ + class Program + { + static void Main(string[] args) + { + try + { + System.String html = ""; + using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(html, "about:blank")) + { + Aspose.Html.Collections.NodeList links = document.QuerySelectorAll("ol.breadcrumb a"); + foreach (Aspose.Html.HTMLElement link in links) + { + System.String href = link.GetAttribute("href"); + System.String text = link.TextContent != null ? link.TextContent.Trim() : System.String.Empty; + System.Console.WriteLine($"Text: {text}, Href: {href}"); + } + } + } + catch (System.Exception ex) + { + System.Console.WriteLine($"Error: {ex.Message}"); + } + } + } +} \ No newline at end of file diff --git a/html-navigation/extract-canonical-link-element-href-value-seo-analysis-document.cs b/html-navigation/extract-canonical-link-element-href-value-seo-analysis-document.cs new file mode 100644 index 0000000..25e1137 --- /dev/null +++ b/html-navigation/extract-canonical-link-element-href-value-seo-analysis-document.cs @@ -0,0 +1,30 @@ +// Extract the canonical link element href value for SEO analysis from the document. + +using System; + +class Program +{ + static void Main() + { + try + { + string htmlContent = "Hello World
"; + Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); + Aspose.Html.Collections.HTMLCollection linkElements = document.GetElementsByTagName("link"); + for (int i = 0; i < linkElements.Length; i++) + { + Aspose.Html.Dom.Element link = linkElements[i]; + string rel = link.GetAttribute("rel"); + if (!string.IsNullOrEmpty(rel) && rel.Equals("canonical", StringComparison.OrdinalIgnoreCase)) + { + string href = link.GetAttribute("href"); + Console.WriteLine($"Canonical href: {href}"); + } + } + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/html-navigation/extract-charset-meta-tag-value-and-ensure-it-matches-file-encoding.cs b/html-navigation/extract-charset-meta-tag-value-and-ensure-it-matches-file-encoding.cs new file mode 100644 index 0000000..b9286d5 --- /dev/null +++ b/html-navigation/extract-charset-meta-tag-value-and-ensure-it-matches-file-encoding.cs @@ -0,0 +1,47 @@ +// Extract the value of the charset meta tag and ensure it matches the file encoding. + +using System; +using System.IO; +using System.Text; +using Aspose.Html; +using Aspose.Html.Dom; + +class Program +{ + static void Main() + { + try + { + string htmlContent = "Hello World!
+ +"; + File.WriteAllText(inputPath, sampleHtml); + } + + using (HTMLDocument document = new HTMLDocument(inputPath)) + { + HTMLCollection metaElements = document.GetElementsByTagName("meta"); + ListHello, world!
+ + "; + + // Load HTML document from string (inline content) + using (HTMLDocument document = new HTMLDocument(htmlContent, "about:blank")) + { + // Get all elements + Aspose.Html.Collections.HTMLCollection elements = document.GetElementsByTagName("meta"); + + bool viewportFound = false; + + for (int i = 0; i < elements.Length; i++) + { + Aspose.Html.HTMLElement element = (Aspose.Html.HTMLElement)elements[i]; + string nameAttr = element.GetAttribute("name"); + if (!string.IsNullOrEmpty(nameAttr) && + string.Equals(nameAttr, "viewport", StringComparison.OrdinalIgnoreCase)) + { + viewportFound = true; + string contentAttr = element.GetAttribute("content") ?? string.Empty; + bool hasWidthDeviceWidth = contentAttr + .IndexOf("width=device-width", StringComparison.OrdinalIgnoreCase) >= 0; + + Console.WriteLine($"Viewport meta tag content: \"{contentAttr}\""); + Console.WriteLine($"Contains width=device-width: {hasWidthDeviceWidth}"); + } + } + + if (!viewportFound) + { + Console.WriteLine("No meta viewport tags were found in the document."); + } + } + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/html-navigation/extract-open-graph-meta-tags-output-dictionary-social-media-integration.cs b/html-navigation/extract-open-graph-meta-tags-output-dictionary-social-media-integration.cs new file mode 100644 index 0000000..0027d1a --- /dev/null +++ b/html-navigation/extract-open-graph-meta-tags-output-dictionary-social-media-integration.cs @@ -0,0 +1,60 @@ +// Extract Open Graph meta tags and output them as a dictionary for social media integration. + +using System; +using System.Collections.Generic; +using Aspose.Html; + +class Program +{ + static void Main() + { + try + { + // Sample HTML content with Open Graph meta tags + string htmlContent = @" + + + + + + + + +Hello
"; + Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(html, "about:blank"); + Aspose.Html.Collections.HTMLCollection metaElements = document.GetElementsByTagName("meta"); + string robotsContent = null; + for (int i = 0; i < metaElements.Length; i++) + { + Aspose.Html.HTMLElement element = (Aspose.Html.HTMLElement)metaElements[i]; + string nameAttr = element.GetAttribute("name"); + if (!string.IsNullOrEmpty(nameAttr) && nameAttr.Equals("robots", StringComparison.OrdinalIgnoreCase)) + { + robotsContent = element.GetAttribute("content"); + break; + } + } + if (robotsContent != null) + { + Console.WriteLine("Robots meta tag content: " + robotsContent); + } + else + { + Console.WriteLine("Robots meta tag not found."); + } + } + catch (Exception ex) + { + Console.WriteLine("Error: " + ex.Message); + } + } +} \ No newline at end of file diff --git a/html-navigation/extract-style-attribute-values-create-stylesheet-file-equivalent-css-rules.cs b/html-navigation/extract-style-attribute-values-create-stylesheet-file-equivalent-css-rules.cs new file mode 100644 index 0000000..4b8dc4c --- /dev/null +++ b/html-navigation/extract-style-attribute-values-create-stylesheet-file-equivalent-css-rules.cs @@ -0,0 +1,63 @@ +// Extract all style attribute values and create a stylesheet file containing equivalent CSS rules. + +using System; +using System.Text; +using System.IO; + +class Program +{ + static void Main() + { + try + { + string htmlContent = @" + + + +Paragraph 1
+ Span + +"; + + var document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); + + var elements = document.QuerySelectorAll("[style]"); + + var cssBuilder = new StringBuilder(); + + for (int i = 0; i < elements.Length; i++) + { + var element = (Aspose.Html.HTMLElement)elements[i]; + string styleValue = element.GetAttribute("style"); + if (string.IsNullOrEmpty(styleValue)) + continue; + + string selector = $"{element.TagName.ToLowerInvariant()}_style_{i + 1}"; + cssBuilder.AppendLine($"{selector} {{{styleValue}}}"); + } + + string cssPath = "extracted-styles.css"; + File.WriteAllText(cssPath, cssBuilder.ToString()); + + var head = document.QuerySelector("head"); + if (head != null) + { + var link = document.CreateElement("link"); + link.SetAttribute("rel", "stylesheet"); + link.SetAttribute("href", cssPath); + head.AppendChild(link); + } + + string outputHtmlPath = "output.html"; + document.Save(outputHtmlPath); + } + catch (Exception ex) + { + Console.WriteLine("Error: " + ex.Message); + } + } +} \ No newline at end of file diff --git a/html-navigation/extract-table-data-html-tables-export-each-table-separate-csv-file.cs b/html-navigation/extract-table-data-html-tables-export-each-table-separate-csv-file.cs new file mode 100644 index 0000000..f94b79b --- /dev/null +++ b/html-navigation/extract-table-data-html-tables-export-each-table-separate-csv-file.cs @@ -0,0 +1,76 @@ +// Extract table data from HTML tables and export each table to a separate CSV file. + +using System; + +class Program +{ + static void Main() + { + try + { + // Sample HTML content with two tables + string htmlContent = "" + + "| Name | Age |
|---|---|
| Alice | 30 |
| Bob | 25 |
| Item1 | 10 |
| Item2 | 20 |
| Header1 | Header2 |
| Row1Col1 | Row1Col2 |
| A |
| B |
| Item | Price |
|---|---|
| Apple | 1.5 |
| Banana | 2.0 |
Paragraph
- Span text - -"; - - // Write the sample HTML to a temporary file - string htmlPath = "sample.html"; - File.WriteAllText(htmlPath, htmlContent, Encoding.UTF8); - - // Load the HTML document - Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlPath); - - // Select all elements that have a style attribute - Aspose.Html.Collections.NodeList elements = document.QuerySelectorAll("[style]"); - - // Build CSS rules from the extracted style attributes - StringBuilder cssBuilder = new StringBuilder(); - - foreach (Aspose.Html.HTMLElement element in elements) - { - // Get the tag name of the element - string tagName = ((Aspose.Html.Dom.Element)element).TagName; - - // Get the value of the style attribute - string styleValue = element.GetAttribute("style"); - - // Append a CSS rule for this tag - cssBuilder.AppendLine($"{tagName} {{{styleValue}}}"); - } - - // Write the generated CSS to a stylesheet file - string cssPath = "extracted-styles.css"; - File.WriteAllText(cssPath, cssBuilder.ToString(), Encoding.UTF8); - - Console.WriteLine($"CSS stylesheet generated at: {Path.GetFullPath(cssPath)}"); - } - catch (Exception ex) - { - Console.WriteLine("An error occurred: " + ex.Message); - } - } -} \ No newline at end of file diff --git a/html-navigation/extract_author_meta_tag_value_and_store_for_content_attribution.cs b/html-navigation/extract_author_meta_tag_value_and_store_for_content_attribution.cs deleted file mode 100644 index 4c25ccd..0000000 --- a/html-navigation/extract_author_meta_tag_value_and_store_for_content_attribution.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Extract the value of the author meta tag and store it for content attribution. - -using System; -using System.Text; -using System.IO; -using Aspose.Html; -using Aspose.Html.Dom; -using Aspose.Html.Collections; - -class Program -{ - static void Main(string[] args) - { - try - { - string htmlContent = ""; - byte[] bytes = System.Text.Encoding.UTF8.GetBytes(htmlContent); - using (var inputStream = new System.IO.MemoryStream(bytes)) - using (var document = new Aspose.Html.HTMLDocument(inputStream, string.Empty)) - { - var metaElements = document.GetElementsByTagName("meta"); - string author = string.Empty; - - foreach (Aspose.Html.Dom.Element meta in metaElements) - { - if (meta.GetAttribute("name") == "author") - { - author = meta.GetAttribute("content"); - break; - } - } - - System.Console.WriteLine("Author meta tag value: " + author); - - string outputPath = "output.html"; - document.Save(outputPath); - } - } - catch (System.Exception ex) - { - System.Console.WriteLine(ex.Message); - } - } -} \ No newline at end of file diff --git a/html-navigation/extract_breadcrumb_navigation_links_locating_ordered_list_elements_specific_class_names.cs b/html-navigation/extract_breadcrumb_navigation_links_locating_ordered_list_elements_specific_class_names.cs deleted file mode 100644 index 2c1b131..0000000 --- a/html-navigation/extract_breadcrumb_navigation_links_locating_ordered_list_elements_specific_class_names.cs +++ /dev/null @@ -1,41 +0,0 @@ -// Extract breadcrumb navigation links by locating ordered list elements with specific class names. - -using System; - -class Program -{ - static void Main() - { - try - { - string html = ""; - using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(html, "")) - { - var breadcrumbLists = document.QuerySelectorAll("ol.breadcrumb"); - var results = new System.Collections.Generic.List| Valid Link |
| Invalid Link |
- Another Valid Link
-
-";
- string inputPath = Path.Combine(Path.GetTempPath(), "sample.html");
- File.WriteAllText(inputPath, htmlContent);
-
- // Load the document
- Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(inputPath);
-
- // 1. Extract links from tables
- HTMLCollection tables = document.GetElementsByTagName("table");
- var tableLinks = new ListHello World
"; - using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(html)) - { - Aspose.Html.Collections.HTMLCollection metas = document.GetElementsByTagName("meta"); - int totalLength = 0; - for (int i = 0; i < metas.Length; i++) - { - Aspose.Html.HTMLElement meta = metas[i] as Aspose.Html.HTMLElement; - if (meta != null) - { - string name = meta.GetAttribute("name"); - if (!string.IsNullOrEmpty(name) && name.Equals("description", StringComparison.OrdinalIgnoreCase)) - { - string content = meta.GetAttribute("content"); - if (!string.IsNullOrEmpty(content)) - { - totalLength += content.Length; - Console.WriteLine($"Description: \"{content}\" (Length: {content.Length})"); - } - } - } - } - Console.WriteLine($"Total description length: {totalLength}"); - } - } - catch (Exception ex) - { - Console.WriteLine($"Error: {ex.Message}"); - } - } -} \ No newline at end of file diff --git a/html-navigation/extract_open_graph_meta_tags_to_dictionary_for_social_media_integration.cs b/html-navigation/extract_open_graph_meta_tags_to_dictionary_for_social_media_integration.cs deleted file mode 100644 index d1c7ece..0000000 --- a/html-navigation/extract_open_graph_meta_tags_to_dictionary_for_social_media_integration.cs +++ /dev/null @@ -1,59 +0,0 @@ -// Extract Open Graph meta tags and output them as a dictionary for social media integration. - -using System; -using System.Collections.Generic; -using Aspose.Html; -using Aspose.Html.Collections; -using Aspose.Html.Dom; - -class Program -{ - static void Main(string[] args) - { - try - { - string html = @" - - - - - -| Header1 | Header2 |
| Row1Col1 | Row1Col2 |
| A | B |
| C | D |
Hello
"; - Aspose.Html.HTMLDocument doc = new Aspose.Html.HTMLDocument(html); - Aspose.Html.Dom.XPath.IXPathResult result = doc.Evaluate("//meta[@name='robots']", doc, doc.CreateNSResolver(doc), Aspose.Html.Dom.XPath.XPathResultType.Any, null); - Aspose.Html.Dom.Node node = result.IterateNext(); - if (node != null) - { - Aspose.Html.Dom.Element element = (Aspose.Html.Dom.Element)node; - string content = element.GetAttribute("content"); - System.Console.WriteLine("Robots meta content: " + content); - } - else - { - System.Console.WriteLine("Robots meta tag not found."); - } - } - catch (System.Exception ex) - { - System.Console.WriteLine("Error: " + ex.Message); - } - } -} \ No newline at end of file diff --git a/html-navigation/find-all-form-elements-list-their-input-field-names-and-default-values-for-analysis.cs b/html-navigation/find-all-form-elements-list-their-input-field-names-and-default-values-for-analysis.cs new file mode 100644 index 0000000..0381f93 --- /dev/null +++ b/html-navigation/find-all-form-elements-list-their-input-field-names-and-default-values-for-analysis.cs @@ -0,0 +1,48 @@ +// Find all form elements, list their input field names and default values for analysis. + +using System; + +class Program +{ + static void Main() + { + try + { + string htmlContent = @""; + + var document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); + + var forms = document.QuerySelectorAll("form"); + + foreach (Aspose.Html.HTMLElement form in forms) + { + var fields = form.QuerySelectorAll("input, select, textarea"); + foreach (Aspose.Html.HTMLElement field in fields) + { + string name = field.GetAttribute("name"); + string value = field.GetAttribute("value"); + string tag = field.TagName.ToLowerInvariant(); + + if (tag == "select") + { + var selectedOption = field.QuerySelector("option[selected]"); + if (selectedOption != null) + { + value = selectedOption.GetAttribute("value"); + } + } + else if (tag == "textarea") + { + value = field.TextContent; + } + + Console.WriteLine($"Field Name: {name}, Default Value: {value}"); + } + } + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + } +} \ No newline at end of file diff --git a/html-navigation/find-element-by-unique-id-modify-inner-text-new-value.cs b/html-navigation/find-element-by-unique-id-modify-inner-text-new-value.cs new file mode 100644 index 0000000..7e2fe65 --- /dev/null +++ b/html-navigation/find-element-by-unique-id-modify-inner-text-new-value.cs @@ -0,0 +1,27 @@ +// Find an element by its unique ID attribute and modify its inner text to a new value. + +using System; + +class Program +{ + static void Main() + { + try + { + string htmlContent = "Old text
"; + var document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); + var elements = document.QuerySelectorAll("#myId"); + foreach (Aspose.Html.HTMLElement element in elements) + { + element.InnerHTML = "New text"; + break; + } + document.Save("output.html"); + Console.WriteLine("Document saved successfully."); + } + catch (Exception ex) + { + Console.WriteLine("Error: " + ex.Message); + } + } +} \ No newline at end of file diff --git a/html-navigation/find_phone_number_patterns_within_text_nodes_output_for_contact_information_extraction.cs b/html-navigation/find-phone-number-patterns-within-text-nodes-output-for-contact-information-extraction.cs similarity index 53% rename from html-navigation/find_phone_number_patterns_within_text_nodes_output_for_contact_information_extraction.cs rename to html-navigation/find-phone-number-patterns-within-text-nodes-output-for-contact-information-extraction.cs index b645d5e..0bb5526 100644 --- a/html-navigation/find_phone_number_patterns_within_text_nodes_output_for_contact_information_extraction.cs +++ b/html-navigation/find-phone-number-patterns-within-text-nodes-output-for-contact-information-extraction.cs @@ -12,19 +12,29 @@ static void Main() { try { - string htmlContent = "Contact: John Doe, Phone: +1 (555) 123-4567. Another: 555-987-6543."; - using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "")) + string html = "Contact us at +1 (555) 123-4567 or 555-987-6543.
Old text
"; - Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument("", htmlContent); - Aspose.Html.Collections.NodeList elements = document.QuerySelectorAll("#myId"); - foreach (Aspose.Html.HTMLElement element in elements) - { - element.InnerHTML = "New text"; - } - string outputPath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "output.html"); - document.Save(outputPath); - System.Console.WriteLine("Document saved to: " + outputPath); - } - catch (System.Exception ex) - { - System.Console.WriteLine("Error: " + ex.Message); - } - } -} \ No newline at end of file diff --git a/html-navigation/generate_json_sitemap_traversing_all_anchor_elements_recording_absolute_urls.cs b/html-navigation/generate-json-sitemap-traversing-all-anchor-elements-recording-absolute-urls.cs similarity index 54% rename from html-navigation/generate_json_sitemap_traversing_all_anchor_elements_recording_absolute_urls.cs rename to html-navigation/generate-json-sitemap-traversing-all-anchor-elements-recording-absolute-urls.cs index d7147fd..a0996c9 100644 --- a/html-navigation/generate_json_sitemap_traversing_all_anchor_elements_recording_absolute_urls.cs +++ b/html-navigation/generate-json-sitemap-traversing-all-anchor-elements-recording-absolute-urls.cs @@ -14,39 +14,21 @@ static void Main() { try { - // Sample HTML content - string htmlContent = @" - -Paragraph
LinkParagraph
LinkHello World
" + + "This is a paragraph with extra spaces.
-Hello World
"; - File.WriteAllText(inputPath, sampleHtml); - } - - // Load the HTML document - Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(inputPath); - - // Get head and body elements - Aspose.Html.HTMLElement head = (Aspose.Html.HTMLElement)Enumerable.First(document.GetElementsByTagName("head")); - Aspose.Html.HTMLElement body = (Aspose.Html.HTMLElement)Enumerable.First(document.GetElementsByTagName("body")); - - // Get all script elements - HTMLCollection scriptElements = document.GetElementsByTagName("script"); - - // Move script elements from head to the end of body - for (int i = 0; i < scriptElements.Length; i++) - { - Aspose.Html.Dom.Element scriptElement = (Aspose.Html.Dom.Element)scriptElements[i]; - if (scriptElement.ParentNode == head) - { - head.RemoveChild(scriptElement); - body.AppendChild(scriptElement); - } - } - - // Save the modified document - document.Save(outputPath); - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - } -} \ No newline at end of file diff --git a/html-navigation/enable_case_sensitive_element_name_matching_xml_like_html_parsing_specific_documents.cs b/html-navigation/parse-html-string-case-insensitive-tag-handling-preserve-original-whitespace-formatting.cs similarity index 57% rename from html-navigation/enable_case_sensitive_element_name_matching_xml_like_html_parsing_specific_documents.cs rename to html-navigation/parse-html-string-case-insensitive-tag-handling-preserve-original-whitespace-formatting.cs index 83f95f3..14b5d41 100644 --- a/html-navigation/enable_case_sensitive_element_name_matching_xml_like_html_parsing_specific_documents.cs +++ b/html-navigation/parse-html-string-case-insensitive-tag-handling-preserve-original-whitespace-formatting.cs @@ -1,4 +1,4 @@ -// Enable case‑sensitive element name matching for XML‑like HTML parsing scenarios when processing specific documents. +// Parse an HTML string with case‑insensitive tag handling and preserve original whitespace formatting. using System; @@ -8,21 +8,23 @@ static void Main() { try { - string htmlContent = "Test
Sample
This is a paragraph.
"; + + // Load HTML document using two‑argument constructor (content, baseUri) + using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank")) + { + // Get the outer HTML of the document + string rawHtml = document.DocumentElement.OuterHTML; + + // Pretty‑print with two‑space indentation + string formattedHtml = PrettyPrintHtml(rawHtml); + + // Output the formatted HTML + Console.WriteLine(formattedHtml); + } + } + catch (Exception ex) + { + Console.Error.WriteLine($"Error: {ex.Message}"); + } + } + + static string PrettyPrintHtml(string html) + { + var sb = new StringBuilder(); + int indent = 0; + string[] tokens = Regex.Split(html, "(<[^>]+>)"); + + foreach (string token in tokens) + { + if (string.IsNullOrWhiteSpace(token)) + continue; + + if (token.StartsWith("<")) + { + if (token.StartsWith("")) + { + indent = Math.Max(indent - 1, 0); + sb.AppendLine(); + sb.Append(new string(' ', indent * 2)); + sb.Append(token); + } + else if (token.EndsWith("/>")) + { + sb.AppendLine(); + sb.Append(new string(' ', indent * 2)); + sb.Append(token); + } + else + { + sb.AppendLine(); + sb.Append(new string(' ', indent * 2)); + sb.Append(token); + indent++; + } + } + else + { + string text = token.Trim(); + if (!string.IsNullOrEmpty(text)) + { + sb.AppendLine(); + sb.Append(new string(' ', indent * 2)); + sb.Append(text); + } + } + } + + return sb.ToString().Trim(); + } +} \ No newline at end of file diff --git a/html-navigation/pretty_print_html_two_space_indentation_per_nesting_level_readability.cs b/html-navigation/pretty_print_html_two_space_indentation_per_nesting_level_readability.cs deleted file mode 100644 index 4a27398..0000000 --- a/html-navigation/pretty_print_html_two_space_indentation_per_nesting_level_readability.cs +++ /dev/null @@ -1,72 +0,0 @@ -// Pretty‑print the HTML with indentation of two spaces per nesting level for readability. - -using System; -using System.Text; -using Aspose.Html; - -class Program -{ - static void Main() - { - try - { - // Sample HTML content - string htmlContent = "World
"; - - // Load HTML into document - using (Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "")) - { - // Get raw outer HTML - string rawHtml = document.DocumentElement.OuterHTML; - - // Pretty‑print with two‑space indentation - string prettyHtml = PrettyPrintHtml(rawHtml, 2); - - // Output result - Console.WriteLine(prettyHtml); - } - } - catch (Exception ex) - { - Console.Error.WriteLine($"Error: {ex.Message}"); - } - } - - static string PrettyPrintHtml(string html, int indentSize) - { - var sb = new StringBuilder(); - int indent = 0; - - for (int i = 0; i < html.Length; i++) - { - if (html[i] == '<') - { - bool isClosingTag = i + 1 < html.Length && html[i + 1] == '/'; - if (isClosingTag) - indent = Math.Max(indent - 1, 0); - - sb.Append('\n'); - sb.Append(new string(' ', indent * indentSize)); - - // Append the whole tag - while (i < html.Length && html[i] != '>') - { - sb.Append(html[i]); - i++; - } - if (i < html.Length) - sb.Append('>'); - - bool isSelfClosing = i - 1 >= 0 && html[i - 1] == '/'; - if (!isClosingTag && !isSelfClosing) - indent++; - } - else - { - sb.Append(html[i]); - } - } - - return sb.ToString().TrimStart('\n'); - } -} \ No newline at end of file diff --git a/html-navigation/remove_all_comment_nodes_from_dom_to_produce_clean_html_output.cs b/html-navigation/remove-all-comment-nodes-from-dom-produce-clean-html-output.cs similarity index 81% rename from html-navigation/remove_all_comment_nodes_from_dom_to_produce_clean_html_output.cs rename to html-navigation/remove-all-comment-nodes-from-dom-produce-clean-html-output.cs index 5bc3db9..79059d5 100644 --- a/html-navigation/remove_all_comment_nodes_from_dom_to_produce_clean_html_output.cs +++ b/html-navigation/remove-all-comment-nodes-from-dom-produce-clean-html-output.cs @@ -7,7 +7,7 @@ class Program { - static void Main() + static void Main(string[] args) { try { @@ -16,12 +16,14 @@ static void Main() if (!File.Exists(inputPath)) { - File.WriteAllText(inputPath, - "Hello
"); + string sampleHtml = "Hello World
"; + File.WriteAllText(inputPath, sampleHtml); } Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(inputPath); + RemoveComments(document.DocumentElement); + document.Save(outputPath); Console.WriteLine("Clean HTML saved to " + outputPath); @@ -38,6 +40,7 @@ static void RemoveComments(Aspose.Html.Dom.Node node) while (child != null) { var next = child.NextSibling; + if (child.NodeName == "#comment") { node.RemoveChild(child); @@ -46,6 +49,7 @@ static void RemoveComments(Aspose.Html.Dom.Node node) { RemoveComments(child); } + child = next; } } diff --git a/html-navigation/remove-all-deprecated-center-tags-replace-with-css-text-align-styling.cs b/html-navigation/remove-all-deprecated-center-tags-replace-with-css-text-align-styling.cs new file mode 100644 index 0000000..613df75 --- /dev/null +++ b/html-navigation/remove-all-deprecated-center-tags-replace-with-css-text-align-styling.cs @@ -0,0 +1,57 @@ +// Remove all deprecatedSome text.
Hello World
\n\t
"; + string htmlContent = "
Text
\n\t
"; - // Load document from string - Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, ""); + // Load document from inline HTML + Aspose.Html.HTMLDocument document = new Aspose.Html.HTMLDocument(htmlContent, "about:blank"); // Get all paragraph elements Aspose.Html.Collections.HTMLCollection paragraphs = document.GetElementsByTagName("p"); @@ -23,24 +20,20 @@ static void Main(string[] args) // Iterate backwards and remove empty paragraphs for (int i = paragraphs.Length - 1; i >= 0; i--) { - Aspose.Html.Dom.Element p = (Aspose.Html.Dom.Element)paragraphs[i]; - if (String.IsNullOrWhiteSpace(p.TextContent)) + Aspose.Html.Dom.Element element = (Aspose.Html.Dom.Element)paragraphs[i]; + if (element.TextContent.Trim().Length == 0 && element.ParentNode != null) { - if (p.ParentNode != null) - { - p.ParentNode.RemoveChild(p); - } + element.ParentNode.RemoveChild(element); } } // Save the modified document string outputPath = "output.html"; document.Save(outputPath); - Console.WriteLine("Document saved to: " + outputPath); } - catch (Exception ex) + catch (System.Exception ex) { - Console.WriteLine("Error: " + ex.Message); + System.Console.WriteLine(ex.Message); } } } \ No newline at end of file diff --git a/html-navigation/remove_all_script_tags_from_dom_eliminate_executable_javascript_before_saving.cs b/html-navigation/remove-all-script-tags-from-dom-to-eliminate-executable-javascript-before-saving.cs similarity index 56% rename from html-navigation/remove_all_script_tags_from_dom_eliminate_executable_javascript_before_saving.cs rename to html-navigation/remove-all-script-tags-from-dom-to-eliminate-executable-javascript-before-saving.cs index 7c084b4..134f816 100644 --- a/html-navigation/remove_all_script_tags_from_dom_eliminate_executable_javascript_before_saving.cs +++ b/html-navigation/remove-all-script-tags-from-dom-to-eliminate-executable-javascript-before-saving.cs @@ -9,39 +9,19 @@ static void Main() { try { - // Input and output file paths - string inputPath = "input.html"; + string inputPath = "sample.html"; string outputPath = "output.html"; - // Create a minimal sample HTML file if it does not exist if (!File.Exists(inputPath)) { - string sampleHtml = @" - - -Content without scripts.
- -"; - File.WriteAllText(inputPath, sampleHtml); + File.WriteAllText(inputPath, + "