// Description: Html Agility Pack - HTML Parsers, selectors, traversors, manupulators. // Website & Documentation: http://html-agility-pack.net // Forum & Issues: https://github.com/zzzprojects/html-agility-pack // License: https://github.com/zzzprojects/html-agility-pack/blob/master/LICENSE // More projects: http://www.zzzprojects.com/ // Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved. #if !METRO using System; using System.IO; using System.Text; namespace HtmlAgilityPack { public partial class HtmlDocument { /// /// Detects the encoding of an HTML document from a file first, and then loads the file. /// /// The complete file path to be read. public void DetectEncodingAndLoad(string path) { DetectEncodingAndLoad(path, true); } /// /// Detects the encoding of an HTML document from a file first, and then loads the file. /// /// The complete file path to be read. May not be null. /// true to detect encoding, false otherwise. public void DetectEncodingAndLoad(string path, bool detectEncoding) { if (path == null) { throw new ArgumentNullException("path"); } Encoding enc; if (detectEncoding) { enc = DetectEncoding(path); } else { enc = null; } if (enc == null) { Load(path); } else { Load(path, enc); } } /// /// Detects the encoding of an HTML file. /// /// Path for the file containing the HTML document to detect. May not be null. /// The detected encoding. public Encoding DetectEncoding(string path) { if (path == null) { throw new ArgumentNullException("path"); } #if NETSTANDARD1_3 || NETSTANDARD1_6 using (StreamReader sr = new StreamReader(File.OpenRead(path), OptionDefaultStreamEncoding)) #else using (StreamReader sr = new StreamReader(path, OptionDefaultStreamEncoding)) #endif { Encoding encoding = DetectEncoding(sr); return encoding; } } /// /// Loads an HTML document from a file. /// /// The complete file path to be read. May not be null. public void Load(string path) { if (path == null) throw new ArgumentNullException("path"); #if NETSTANDARD1_3 || NETSTANDARD1_6 using (StreamReader sr = new StreamReader(File.OpenRead(path), OptionDefaultStreamEncoding)) #else using (StreamReader sr = new StreamReader(path, OptionDefaultStreamEncoding)) #endif { Load(sr); } } /// /// Loads an HTML document from a file. /// /// The complete file path to be read. May not be null. /// Indicates whether to look for byte order marks at the beginning of the file. public void Load(string path, bool detectEncodingFromByteOrderMarks) { if (path == null) throw new ArgumentNullException("path"); #if NETSTANDARD1_3 || NETSTANDARD1_6 using (StreamReader sr = new StreamReader(File.OpenRead(path), detectEncodingFromByteOrderMarks)) #else using (StreamReader sr = new StreamReader(path, detectEncodingFromByteOrderMarks)) #endif { Load(sr); } } /// /// Loads an HTML document from a file. /// /// The complete file path to be read. May not be null. /// The character encoding to use. May not be null. public void Load(string path, Encoding encoding) { if (path == null) throw new ArgumentNullException("path"); if (encoding == null) throw new ArgumentNullException("encoding"); #if NETSTANDARD1_3 || NETSTANDARD1_6 using (StreamReader sr = new StreamReader(File.OpenRead(path), encoding)) #else using (StreamReader sr = new StreamReader(path, encoding)) #endif { Load(sr); } } /// /// Loads an HTML document from a file. /// /// The complete file path to be read. May not be null. /// The character encoding to use. May not be null. /// Indicates whether to look for byte order marks at the beginning of the file. public void Load(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks) { if (path == null) throw new ArgumentNullException("path"); if (encoding == null) throw new ArgumentNullException("encoding"); #if NETSTANDARD1_3 || NETSTANDARD1_6 using (StreamReader sr = new StreamReader(File.OpenRead(path), encoding, detectEncodingFromByteOrderMarks)) #else using (StreamReader sr = new StreamReader(path, encoding, detectEncodingFromByteOrderMarks)) #endif { Load(sr); } } /// /// Loads an HTML document from a file. /// /// The complete file path to be read. May not be null. /// The character encoding to use. May not be null. /// Indicates whether to look for byte order marks at the beginning of the file. /// The minimum buffer size. public void Load(string path, Encoding encoding, bool detectEncodingFromByteOrderMarks, int buffersize) { if (path == null) throw new ArgumentNullException("path"); if (encoding == null) throw new ArgumentNullException("encoding"); #if NETSTANDARD1_3 || NETSTANDARD1_6 using (StreamReader sr = new StreamReader(File.OpenRead(path), encoding, detectEncodingFromByteOrderMarks, buffersize)) #else using (StreamReader sr = new StreamReader(path, encoding, detectEncodingFromByteOrderMarks, buffersize)) #endif { Load(sr); } } /// /// Saves the mixed document to the specified file. /// /// The location of the file where you want to save the document. public void Save(string filename) { #if NETSTANDARD1_3 || NETSTANDARD1_6 using (StreamWriter sw = new StreamWriter(File.OpenWrite(filename), GetOutEncoding())) #else using (StreamWriter sw = new StreamWriter(filename, false, GetOutEncoding())) #endif { Save(sw); } } /// /// Saves the mixed document to the specified file. /// /// The location of the file where you want to save the document. May not be null. /// The character encoding to use. May not be null. public void Save(string filename, Encoding encoding) { if (filename == null) { throw new ArgumentNullException("filename"); } if (encoding == null) { throw new ArgumentNullException("encoding"); } #if NETSTANDARD1_3 || NETSTANDARD1_6 using (StreamWriter sw = new StreamWriter(File.OpenWrite(filename), encoding)) #else using (StreamWriter sw = new StreamWriter(filename, false, encoding)) #endif { Save(sw); } } } } #endif