Reputation: 1493
I am really having a hard time navigating through all the properties in the HTMLDocument
class. I have a Web page I am loading in a WPF WebBrowser
embedded in a Window
. I have an event like
private void _browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
HTMLDocument document = (HTMLDocument) this._browser.Document;
if (document.body != null)
{
this.Height = //get the div with the id 'wrapper' and get it's height
}
}
Basically. when the page is done with navigating, I want to get the height of a specific divider in the HTML.
Here is how the HTML looks like:
<html lang="en-US">
<head>…</head>
<div id = "wrapper">…</div>
<body blah blah blah>…</body>
</html>
It seems that I can't find this HTML, though it is in the HTMLDocument
class. I want to get the height of this divider and set the window height to the height of this divider plus whatever other space for buttons and other things are required.
Where should I search for?
At first, I was trying
HTMLDocument document = (HTMLDocument) this._browser.Document;
this.Height= document.body.offsetHeight
but this was nowhere near correct, and I can't seem to find anything of the type HTMLElement
that I have read, where you can access individual HTMLElement
s.
If you have worked with this class before or can see where I am going wrong, any help would be greatly appreciated.
Upvotes: 1
Views: 2237
Reputation: 1940
Please download the HTML Agility Pack and add it to project references
protected void BtnIndex_Click(object sender, EventArgs e)
{
string source = getHtml("http://stackoverflow.com");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(source);
HtmlNodeCollection cont;
cont = doc.DocumentNode.SelectNodes("//div[@id='wrapper']");
}
string getHtml(string url)
{
string Shtml = string.Empty; ;
try
{
//WebClient client = new WebClient();
//client.Encoding = Encoding.UTF8;
//Shtml = client.DownloadString(url);
//Shtml = new WebClient().DownloadString(url); bejoz utf-8
HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
myWebRequest.Method = "GET";
// Make request for web page
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());
Shtml = myWebSource.ReadToEnd();
myWebResponse.Close();
}
catch
{
Response.Write(" error: " + url + Environment.NewLine);
}
return Shtml;
}
Upvotes: 1