WPF WebBrowser how best to get Tag trees

74 Views Asked by At

Can anyone show me how can i write this code correctly in WPF

var rows = webBrowser1.Document.GetElementsByTagName("table")["myTable"].GetElementsByTagName("tr");

for(int i = 0; i < rows.Count; i++)
{
    MessageBox.Show(rows[i].GetElementsByTagName("td")[0].ToString());
}
2

There are 2 best solutions below

0
On BEST ANSWER

You could always host the System.Windows.Forms.WebBrowser in a System.Windows.Forms.Integration.WindowsFormsHost control:

Use the WindowsFormsHost element to place a Windows Forms control within your WPF element or page.

<WindowsFormsHost HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Forms:WebBrowser Name="webBrowser1"/>
</WindowsFormsHost>
0
On

I'm guessing you're reading HTML and not XML here?

GetElementsByTagName only takes one argument of type string. Thus, you must search for only one type of element (ie table, td, etc) at a time. You cannot search for multiple types of elements at the same time. Also this only searches for tags. For attributes (such as myTable), you must use use the GetAttribute method or something similar.

I would check the MSDN documentation for more info.