Public class HtmlAgilityPackService
{
private XmlDocument _xmldoc = new XmlDocument();
private string _path = Directory.GetCurrentDirectory();
private HtmlAgilityPack.HtmlDocument _doc = new HtmlAgilityPack.HtmlDocument();
private WebBrowser _web = new WebBrowser();
public HtmlAgilityPackService(string URL)
{
//讀取XML檔,指定目錄位置
_xmldoc.Load(_path + "\\PathStructure.xml");
_web.ScriptErrorsSuppressed = true;
_web.Navigate(URL);
waitTillLoad(_web);//取得HTML值
_doc.Load(new StringReader(_web.Document.Body.OuterHtml));
}
//讀取html ,值到讀取狀態complete
private void waitTillLoad(WebBrowser webBrControl)
{
WebBrowserReadyState loadStatus;
int waittime = 10;
int counter = 0;
while (true)
{
loadStatus = webBrControl.ReadyState;
Application.DoEvents();
if ((counter > waittime) ||
(loadStatus == WebBrowserReadyState.Uninitialized) ||
(loadStatus == WebBrowserReadyState.Loading) ||
(loadStatus == WebBrowserReadyState.Interactive))
{
break;
}
counter++;
}
counter = 0;
while (true)
{
loadStatus = webBrControl.ReadyState;
Application.DoEvents();
if ((loadStatus == WebBrowserReadyState.Complete||
loadStatus==WebBrowserReadyState.Interactive)
&& webBrControl.IsBusy != true)
{
break;
}
counter++;
}
}
//取得產品名稱
public string GetName()
{
try
{
XmlNode productName = _xmldoc.DocumentElement.SelectSingleNode("/etmall/ProductName");
HtmlNodeCollection name = _doc.DocumentNode.SelectNodes(productName.InnerText);
if (name.Count > 0)
return name[0].InnerText;
else
return "error";
}
catch (Exception)
{
return "error";
}
}
//取得產品圖片
public string GetImage()
{
try
{
XmlNode xmlImage =
_xmldoc.DocumentElement.SelectSingleNode("/etmall/ProductImg");
//取得XML檔案的XPATH設定值
HtmlNodeCollection image = _d
doc.DocumentNode.SelectNodes(xmlImage.InnerText);
if (image.Count > 0)
{
HtmlAttribute imageAttribute = image[0].Attributes["src"];
return imageAttribute.Value; //傳回IMAGE的來源位址
}
else {
return "error";
}
}
catch (Exception)
{
return "error";
}
}
}
|