I Want To Retrieve The Width And Height Of Html Element With Htmlagilitypack
How to retrieve the width and height of img element with HtmlAgilityPack I do this like this.. HtmlAgilityPack.HtmlAttribute width = link.Attributes['width']; HtmlAgilityPa
Solution 1:
Basead in this page: page
publicsealedclassUtilParserHTML
{
//Private Fieldsprivate Uri Uri;
private Stream StreamPage;
private HttpWebRequest HttpRequest;
private HttpWebResponse HttpResponse;
//Public Fieldspublic HtmlDocument HtmlDocument { privateset; get; }
publicUtilParserHTML()
{
if (this.HtmlDocument == null)
HtmlDocument = new HtmlDocument();
}
publicvoidLoadHTMLPage(string UrlPage)
{
if (string.IsNullOrEmpty(UrlPage))
thrownew ArgumentNullException("");
CookieContainer cookieContainer = new CookieContainer();
this.Uri = new Uri(UrlPage);
this.HttpRequest = (HttpWebRequest)WebRequest.Create(UrlPage);
this.HttpRequest.Method = WebRequestMethods.Http.Get;
this.HttpRequest.CookieContainer = cookieContainer;
this.HttpResponse = (HttpWebResponse)this.HttpRequest.GetResponse();
this.StreamPage = this.HttpResponse.GetResponseStream();
this.HtmlDocument.Load(StreamPage);
}
publicvoidLoadHTMLPage(FileStream StreamPage)
{
if (StreamPage == null)
thrownew ArgumentNullException("");
HtmlDocument.Load(StreamPage);
}
public HtmlNodeCollection GetNodesByExpression(string XPathExpression)
{
if (string.IsNullOrEmpty(XPathExpression))
thrownew ArgumentNullException("");
returnthis.HtmlDocument.DocumentNode.SelectNodes(XPathExpression);
}
Use XPath to navigate in html. In this case I used this Xpath expression : //div[@class='arrowRibbon'] //img
Look: ...
this.ParserHTML.LoadHTMLPage("http://www.img.com.br/default.aspx");
HtmlNodeCollection HtmlNodeCollectionResult = this.ParserHTML.GetNodesByExpression(Page.XPathExpression);
if (HtmlNodeCollectionResult != null)
{
foreach (HtmlNode NodeResult in HtmlNodeCollectionResult)
{
var src = NodeResult.Attributes["src"].Value;
}
}
...
EDIT
Look this complete example with width and height:
this.ParserHTML.LoadHTMLPage("http://www.w3schools.com/tags/tag_img.asp");
HtmlNodeCollection HtmlNodeCollectionResult = this.ParserHTML.GetNodesByExpression("//div[@class='tryit_ex'] //img");
if (HtmlNodeCollectionResult != null)
{
foreach (HtmlNode NodeResult in HtmlNodeCollectionResult)
{
var src = NodeResult.Attributes["src"].Value;
var w = NodeResult.Attributes["width"].Value;
var h = NodeResult.Attributes["height"].Value;
}
}
Hope this help.
Post a Comment for "I Want To Retrieve The Width And Height Of Html Element With Htmlagilitypack"