Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was wondering if it is possible to get 'All' the ID's in an html document or at least all the text box's; on say, a signup form. Now I have heard of the HTML Agility Pack but I don't even know where to start with setting it up. I was wondering if the mshtml parser could accomplish this, if so how. Now I have somewhat did this in VB.NET where I would identify the id manually because I already knew the name, but I was hoping to get all ID's and determine which one was a textbox; combobox radio button; checkbox etc..

This is what I have in VB.NET
VB
For Each currentElement As HtmlElement In theElementCollection
        Dim controlName As String = currentElement.GetAttribute("id").ToString
        If controlName = "SearchForm1_txtEmpID" Then
                   currentElement.SetAttribute("Value", _
                   securityQA.tbFindUser.Text.Remove(0, 3).Trim(" "))
                   End If
Next


And of course converted to C#:

C#
foreach (HtmlElement currentElement in theElementCollection) {
	string controlName = currentElement.GetAttribute("id").ToString;
	if (controlName == "SearchForm1_txtEmpID") {
		currentElement.SetAttribute("Value", securityQA.tbFindUser.Text.Remove(0, 3).Trim(" "));
	}
}


And this is just manually setting the controlName. I want to get all elements possible through a recursive loop and then add them to an array and be able to set them attribute later when its called for. Of course this will also be used with a webbrowser control to.
Posted
Comments
walterhevedeich 9-Aug-11 1:56am    
How bout using the TagName property of the HtmlElement? Would that work for you?

1 solution

Hello!

In hope the question is not already solved and I understand your problem, here my recommendation:
Using the HTML Agility Pack is a good idea, it is almost like the Microsoft XmlDocument but supports html (i.e. it does not crash if the html is malformed).
The solution to your problem might be XPath (here is a really good tutorial) - with a single XPath-expression you can enumerate through all your elements which have an id:

C#
HtmlDocument hd = new HtmlDocument();
//Load your stuff with hd.Load(Stream, Encoding)

foreach (var node in hd.DocumentNode.SelectNodes("//input[@id]"))
  //Select all input-Elements which have an id-attribute
{
  //Here you can do what you want with your node
}
 
Share this answer
 
Comments
Amir Mahfoozi 26-Oct-11 7:37am    
I did the same thing with Html Agility Pack. it is brilliant. +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900