如何在 C# 中加载 HTML

HTML 加载是各种 Web 任务的基本操作,包括 Web 开发、网页渲染、数据提取、内容管理、文档处理、测试等。 Aspose.HTML for .NET 库为 HTMLDocument 类提供一组 HTMLDocument() 构造函数,用于加载 HTML 并初始化 HTMLDocument 对象以供将来操作。 HTML 文档可以从文件或 URL 加载,也可以从字符串或内存流创建和加载。那么,让我们看看加载 HTML 的方法!

首先,确保您的项目中安装了 Aspose.HTML for .NET。这个库的安装过程比较简单。您可以使用以下命令通过 NuGet 包管理器控制台安装它:


安装 Aspose.HTML for .NET

Install-Package Aspose.HTML

从文件加载 HTML

从文件加载 HTML 是处理现有 HTML 文件、模板或以 HTML 格式存储的数据的良好起点。如果您需要从文件加载现有 HTML 文件,对其进行处理并保存,那么以下代码片段将帮助您:

  1. 使用 HTMLDocument(address) 构造函数从文件加载 HTML 文档,该构造函数从地址(本地文档路径)加载 HTML 文档。
  2. 使用 Save() 方法保存 HTML 文件。

Aspose.HTML for .NET 库提供了一组构造函数,允许您从文件加载 HTML 文档。例如, HTMLDocument(address, configuration) 从具有指定环境配置设置的地址加载HTML文档。有关详细信息,请参阅 API 参考 HTMLDocument 章节。


从文件加载 HTML 的 C# 代码

using System.IO;
using Aspose.Html;
...

    // Prepare a file path
	string documentPath = Path.Combine(DataDir, "sprite.html");

	// Initialize an HTML document from the file
	using (var document = new HTMLDocument(documentPath))
	{
		// Work with the document

		// Save the document to a disk
		document.Save(Path.Combine(OutputDir, "sprite_out.html"));
	}

从 URL 加载 HTML

当您需要从网页中提取信息时,从 URL 加载 HTML 会很有用。您可以直接从 URL 加载 HTML:

  1. 使用 HTMLDocument(Url) 构造函数从 URL 加载 HTML 文档。如果您想从具有指定环境配置设置的 URL 加载 HTML,可以使用 HTMLDocument(Url, configuration)
  2. 使用 OuterHTM 属性获取文档的完整 HTML 内容。这包括整个文档的 HTML,包括 HTML 元素本身。

如果您需要将 HTML 文档保存在本地驱动器上,请使用 Save() 方法。


从 URL 加载 HTML 的 C# 代码

using System.IO;
using Aspose.Html;
...

	// Load a document from 'https://docs.aspose.com/html/net/creating-a-document/' web page
	using (var document = new HTMLDocument("https://docs.aspose.com/html/net/creating-a-document/"))
	{
		var html = document.DocumentElement.OuterHTML;

		// Write the document content to the output stream
		Console.WriteLine(html);
	}

从字符串加载 HTML

从字符串加载 HTML 是一项重要功能,它允许您操作 HTML 内容并将非结构化 HTML 字符串转换为可以操作、解析或显示的结构化文档:

  1. 首先,准备 HTML 文档的代码。
  2. 使用 HTMLDocument(content, baseUri) 构造函数从具有指定 baseUri 的字符串内容初始化 HTML 文档。
  3. 使用 Save() 方法保存 HTML 文件。

从字符串加载 HTML 的 C# 代码

using System.IO;
using Aspose.Html;
...

	// Prepare HTML code
    var html_code = "<p>Learn how to load HTML</p>";

    // Initialize a document from the string variable
    using (var document = new HTMLDocument(html_code, "."))
    {
        // Save the document to a disk
        document.Save(Path.Combine(OutputDir, "load-html-from-string.html"));
    }

从内存流加载 HTML

从流加载 HTML 对于提高内存效率或处理内存中的数据很有用。以下 C# 代码演示了如何从 MemoryStream 加载 HTML 并使用 Aspose.HTML for .NET 将其保存到文件中:

  1. 初始化MemoryStreamStreamWriter对象。 StreamWriter用于将HTML代码写入MemoryStream。
  2. 使用 Write() 方法将 HTML 代码写入 MemoryStream。
  3. 调用Flush()以确保所有缓冲数据都写入流中,并使用Seek(0, SeekOrigin.Begin)将流的位置设置到开头。这很重要,因为 HTMLDocument 从流中的当前位置读取内容。
  4. 使用 HTMLDocument(content, baseUri) 构造函数从 MemoryStream 初始化HTMLDocument。 HTMLDocument 的实例是通过将 MemoryStream 对象和基本 URI 作为参数传递来创建的。
  5. 使用 Save() 方法将 HTML 文件保存到本地驱动器。

从内存流加载 HTML 的 C# 代码

using System.IO;
using Aspose.Html;
...

    // Create a memory stream object
	using (var mem = new MemoryStream())
	using (var sw = new StreamWriter(mem))
	{
		// Write the HTML code into the memory object
		sw.Write("<p>Load HTML from a memory stream</p>");

		// It is important to set the position to the beginning, since HTMLDocument starts the reading exactly from the current position within the stream
		sw.Flush();
		mem.Seek(0, SeekOrigin.Begin);

		// Initialize a document from the string variable
		using (var document = new HTMLDocument(mem, "."))
		{
			// Save the document to a local disk
			document.Save(Path.Combine(OutputDir, "load-html-from-stream.html"));
		}
	}

要了解有关 Aspose.HTML API 的更多信息,请访问我们的 文档 指南。从 Create HTML Document 文章中,您将找到有关如何从文件、URL 和流加载文档或从头开始创建文档的信息。 Edit HTML Document 文档文章为您提供了有关如何使用 Aspose.HTML for .NET API 读取或编辑文档对象模型的基本信息。您将探索如何创建 HTML 元素以及如何使用它们 – 通过插入新节点、删除或编辑现有节点的内容来修改文档。