faritia coralee glynndon

0 views
Skip to first unread message

Teena Ruiter

unread,
Aug 3, 2024, 7:28:51 PM8/3/24
to lecthenehux

How to Use Aspose.HTML for .NET v19.10 to Perform HTML Manipulation Tasks

Aspose.HTML for .NET v19.10 is a cross-platform class library that enables your applications to perform a wide range of HTML manipulation tasks. Since HTML is generally used in combination with other technologies, Aspose.HTML supports not only HTML5 but CSS3, SVG, and HTML Canvas specifications as well[^1^].

In this article, we will show you how to use Aspose.HTML for .NET v19.10 to perform some common HTML manipulation tasks, such as:

    • Loading and saving HTML documents from various sources
    • Removing and replacing HTML nodes
    • Extracting CSS from HTML
    • Configuring a document sandbox
    • Converting HTML to other formats

    Loading and Saving HTML Documents from Various Sources

    Aspose.HTML for .NET v19.10 allows you to create or open existing HTML documents from various sources, such as files, streams, URLs, or strings. You can use the HTMLDocument class to load an HTML document from any source and save it to any supported output format. For example, the following code snippet shows how to load an HTML document from a file and save it as a PDF file:

    ```csharp
    // Create an instance of HTMLDocument class
    var document = new Aspose.Html.HTMLDocument("input.html");
    // Save the document as PDF file
    document.Save("output.pdf", Aspose.Html.Saving.PdfSaveOptions.Default);
    ```

    Removing and Replacing HTML Nodes

    Aspose.HTML for .NET v19.10 allows you to manipulate the HTML document tree by inserting, removing, and editing the nodes. You can use the Node class and its derived classes to access and modify the nodes of an HTML document. For example, the following code snippet shows how to remove all the <script> elements from an HTML document:

    ```csharp
    // Create an instance of HTMLDocument class
    var document = new Aspose.Html.HTMLDocument("input.html");
    // Get all the script elements
    var scripts = document.GetElementsByTagName("script");
    // Loop through the script elements
    foreach (var script in scripts)
    // Remove the script element from its parent node
    script.ParentNode.RemoveChild(script);
    // Save the modified document
    document.Save("output.html");
    ```

    Extracting CSS from HTML

    Aspose.HTML for .NET v19.10 allows you to extract CSS styling information from an HTML document. You can use the CSSStyleSheet class and its related classes to access and modify the CSS rules of an HTML document. For example, the following code snippet shows how to extract all the CSS rules from an HTML document and save them as a separate file:

    ```csharp
    // Create an instance of HTMLDocument class
    var document = new Aspose.Html.HTMLDocument("input.html");
    // Get all the style sheets of the document
    var styleSheets = document.StyleSheets;
    // Create a string builder to store the CSS rules
    var cssBuilder = new System.Text.StringBuilder();
    // Loop through the style sheets
    foreach (var styleSheet in styleSheets)
    // Cast the style sheet to CSSStyleSheet class
    var cssStyleSheet = styleSheet as Aspose.Html.Dom.Css.CSSStyleSheet;
    // Check if the style sheet is not null
    if (cssStyleSheet != null)
    // Get all the CSS rules of the style sheet
    var cssRules = cssStyleSheet.CssRules;
    // Loop through the CSS rules
    foreach (var cssRule in cssRules)
    // Append the CSS rule to the string builder
    cssBuilder.AppendLine(cssRule.CssText);
    // Save the CSS rules as a file
    System.IO.File.WriteAllText("output.css", cssBuilder.ToString());
    ```

    Configuring a Document Sandbox

    Aspose.HTML for .NET v19.10 allows you to configure a document sandbox for the environment independent of the execution machine. You can use the Sandbox class and its properties to control various aspects of the

    51082c0ec5
    Reply all
    Reply to author
    Forward
    0 new messages