當我嘗試從文件系統加載文件時,我遇到了問題。問題在於某些HTML控件的值,我在span值內部標記“<”
HtmlDocument doc = new HtmlDocument();
doc.OptionReadEncoding = true;
//StreamReader str = new StreamReader(fileName, Encoding.UTF8);
StreamReader str = new StreamReader(@"E:\HTMLS\OEL\1030,1.html",Encoding.UTF8,true);
doc.Load(str.BaseStream, Encoding.ASCII);
//string streamString = str.ReadToEnd().
str.Close();
//all nodes
doc.DocumentNode.Descendants().Where(x => x.Name == "#text" && (x.InnerText == "\r\n\t" || x.InnerText == "\r\n" || x.InnerText == "\r\n\t\t")).ToList().ForEach(x => x.Remove());
List<HtmlNode> listHtmlNode = doc.DocumentNode.Descendants("table").ToList();
您不應該在HTML中使用<
作為內容的符號。將它們放在html中會使html無效,並導致HTMLAgility包無法正常執行。
如果你需要在html中使用它們,你需要對它們進行編碼。 <
變成%lt;
請參閱http://www.w3schools.com/html/html_entities.asp