HtmlDocument 实用工具
HtmlDocument 方法
名称 | 描述 |
---|---|
DetectEncoding(Stream) | Detects the encoding of an HTML stream. |
DetectEncoding(TextReader) | Detects the encoding of an HTML text provided on a TextReader. |
DetectEncoding(String) | Detects the encoding of an HTML file. |
DetectEncodingAndLoad(String) | Detects the encoding of an HTML document from a file first, and then loads the file. |
DetectEncodingAndLoad(String, Boolean) | Detects the encoding of an HTML document from a file first, and then loads the file. |
public Encoding DetectEncoding(Stream stream)
检测HTML流的编码。DetectEncoding
方法是HtmlAgilityPack.HtmlDocument 的成员
参数:
stream: 输入流。 不可能为null。
返回:
检测到的编码。
以下示例检测HTML流的编码。
var htmlNewDoc = new HtmlDocument();
FileStream newfs = new FileStream("Test.html", FileMode.Open);
var encoding = htmlNewDoc.DetectEncoding(newfs);
public Encoding DetectEncoding(TextReader reader)
检测TextReader
上提供的HTML文本的编码。DetectEncoding
方法是HtmlAgilityPack.HtmlDocument 的成员
参数:
reader: 用于提供HTML的TextReader
。 不可能为null。
返回:
检测到的编码。
以下示例检测TextReader
上提供的HTML文本的编码。
var htmlNewDoc = new HtmlDocument();
TextReader tr = File.OpenText("Test.html");
var encoding = htmlNewDoc.DetectEncoding(tr);
public Encoding DetectEncoding(string path)
检测HTML文件的编码。DetectEncoding
方法是HtmlAgilityPack.HtmlDocument 的成员
参数:
path: 包含要检测的HTML文档的文件的路径。 不可能为null。
返回:
检测到的编码。
以下示例检测HTML文件的编码。
var htmlNewDoc = new HtmlDocument();
var encoding = htmlNewDoc.DetectEncoding("Test.html");
public void DetectEncodingAndLoad(string path)
首先从文件中检测HTML文档的编码,然后加载文件。 DetectEncodingAndLoad
方法是HtmlAgilityPack.HtmlDocument 的成员
参数:
path: 要读取的完整文件路径。
以下示例首先检测HTML文件的编码,然后加载它。
var htmlNewDoc = new HtmlDocument();
htmlNewDoc.DetectEncodingAndLoad("Test.html");
public void DetectEncodingAndLoad(string path, bool detectEncoding)
Detects the encoding of an HTML document from a file first, and then loads the file. DetectEncodingAndLoad method is a member of HtmlAgilityPack.HtmlDocument
首先从文件中检测HTML文档的编码,然后加载文件。 DetectEncodingAndLoad
方法是HtmlAgilityPack.HtmlDocument 的成员
参数:
path: 要读取的完整文件路径。不可能为null。
detectEncoding: 检测编码为true,否则为false。
以下示例首先检测HTML文件的编码,然后加载它。
var htmlNewDoc = new HtmlDocument();
htmlNewDoc.DetectEncodingAndLoad("Test.html", true);