You need to set UseIdAttribute property to true to enable this feature

Topics: Developer Forum, User Forum
Jun 27, 2011 at 6:02 PM

I'm just starting with HTML Agility Pack with visual studio express, When I tried to use the GetElementById method, I got a runtime error "You need to set UseIdAttribute property to true to enable this feature".  I've searched, but I can't find what useidattribute is a property of. Help? TIA!

Jun 27, 2011 at 7:24 PM

The "OptionUseIdAttribute" field on the HtmlDocument class needs to be set to true. It is set to true by default though. Do you have an example?

Jun 27, 2011 at 8:31 PM
Edited Jun 27, 2011 at 8:33 PM

I did see the "OptionUsedIdAttribute", and it was set to True already. Here is some code - "MainContentArea = myDoc.GetElementbyId("main")"is throwing the error. Thanks for taking a look.


Imports HtmlAgilityPack

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myWeb As HtmlWeb = New HtmlWeb
        Dim myDoc As HtmlDocument = New HtmlDocument
        Dim myRoot As HtmlNode = myDoc.DocumentNode
        Dim myElements As List(Of HtmlElement) = New List(Of HtmlElement)
        Dim MainContentArea As HtmlNode

        MainContentArea = myDoc.GetElementbyId("main")

        TextBox1.Text = MainContentArea.InnerHtml
    End Sub

End Class
Jun 27, 2011 at 8:35 PM

ahh.. I see the problem. The myDoc is an empty HtmlDocument. The myWeb.Load returns the HtmlDocument object you want. So you'll want to do 

Dim myDoc = myWeb.Load("")

Jun 27, 2011 at 9:03 PM

Thanks That fixed it.

Jun 29, 2011 at 2:51 PM
Edited Jun 29, 2011 at 2:56 PM

[SOLVED]: I noticed the problem immediately after posting this. Notice my catch just silently eats the error. I have error handling on the other side but it's for null objects (I use other methods other than the HTML agility pack).


I'm having a similar problem with C#.

The following method is used to get the htmlDocument.


public HtmlDocument DownloadHtmlDocument(string address)
				var htmlDocument = new HtmlDocument {OptionUseIdAttribute = true};
					using (var stream = OpenRead(address))
						if (stream != null) stream.Close();
				catch (Exception e)
					new Logger().Log("address value: " + address + "\n" + e.Message, e);
				return htmlDocument;


This code uses the htmlDocument,GetElementById().


var htmlDocument = _webClient.DownloadHtmlDocument(source.URL);
 foreach (var htmlElementPatch in htmlDocument.GetElementbyId("table").SelectNodes("tbody/tr"))


The funny thing is the same method above is used in the same way for two different websites in two different assemblies and it works for one and not the other. Any suggestions?

Does the HTMLDocument.Load(string url) method as mentioned by darthobiwan do essentially the same thing?